FD.io VPP  v21.01.1
Vector Packet Processing
api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 
21 #include <af_xdp/af_xdp.h>
22 
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 
26 /* define message IDs */
27 #include <af_xdp/af_xdp.api_enum.h>
28 #include <af_xdp/af_xdp.api_types.h>
29 
31 
32 static af_xdp_mode_t
33 af_xdp_api_mode (vl_api_af_xdp_mode_t mode)
34 {
35  switch (mode)
36  {
38  return AF_XDP_MODE_AUTO;
40  return AF_XDP_MODE_COPY;
42  return AF_XDP_MODE_ZERO_COPY;
43  }
44  return AF_XDP_MODE_AUTO;
45 }
46 
47 static void
49 {
54  int rv;
55 
56  clib_memset (&args, 0, sizeof (af_xdp_create_if_args_t));
57 
58  args.linux_ifname = mp->host_if[0] ? (char *) mp->host_if : 0;
59  args.name = mp->name[0] ? (char *) mp->name : 0;
60  args.prog = mp->prog[0] ? (char *) mp->prog : 0;
61  args.mode = af_xdp_api_mode (mp->mode);
62  args.rxq_size = ntohs (mp->rxq_size);
63  args.txq_size = ntohs (mp->txq_size);
64  args.rxq_num = ntohs (mp->rxq_num);
65 
66  af_xdp_create_if (vm, &args);
67  rv = args.rv;
68 
69  /* *INDENT-OFF* */
70  REPLY_MACRO2 (VL_API_AF_XDP_CREATE_REPLY + rm->msg_id_base,
71  ({
72  rmp->sw_if_index = ntohl (args.sw_if_index);
73  }));
74  /* *INDENT-ON* */
75 }
76 
77 static void
79 {
81  vnet_main_t *vnm = vnet_get_main ();
83  vl_api_af_xdp_delete_reply_t *rmp;
84  af_xdp_device_t *rd;
86  int rv = 0;
87 
88  hw =
90  htonl (mp->sw_if_index));
91  if (hw == NULL || af_xdp_device_class.index != hw->dev_class_index)
92  {
93  rv = VNET_API_ERROR_INVALID_INTERFACE;
94  goto reply;
95  }
96 
97  rd = pool_elt_at_index (rm->devices, hw->dev_instance);
98 
99  af_xdp_delete_if (vm, rd);
100 
101 reply:
102  REPLY_MACRO (VL_API_AF_XDP_DELETE_REPLY + rm->msg_id_base);
103 }
104 
105 /* set tup the API message handling tables */
106 #include <af_xdp/af_xdp.api.c>
107 static clib_error_t *
109 {
110  af_xdp_main_t *rm = &af_xdp_main;
111 
112  /* ask for a correctly-sized block of API message decode slots */
114  return 0;
115 }
116 
118 
119 /*
120  * fd.io coding-style-patch-verification: ON
121  *
122  * Local Variables:
123  * eval: (c-set-style "gnu")
124  * End:
125  */
u16 msg_id_base
Definition: af_xdp.h:116
#define ntohs(x)
Definition: af_xdp.bpf.c:29
vnet_device_class_t af_xdp_device_class
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vl_api_interface_index_t sw_if_index
Definition: af_xdp.api:82
static af_xdp_mode_t af_xdp_api_mode(vl_api_af_xdp_mode_t mode)
Definition: api.c:33
string host_if[64]
Definition: af_xdp.api:46
vlib_main_t * vm
Definition: in2out_ed.c:1580
void af_xdp_delete_if(vlib_main_t *vm, af_xdp_device_t *ad)
Definition: device.c:89
static clib_error_t * af_xdp_plugin_api_hookup(vlib_main_t *vm)
Definition: api.c:108
af_xdp_device_t * devices
Definition: af_xdp.h:114
static void vl_api_af_xdp_create_t_handler(vl_api_af_xdp_create_t *mp)
Definition: api.c:48
af_xdp_main_t af_xdp_main
Definition: device.c:30
af_xdp_mode_t
Definition: af_xdp.h:121
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
#define REPLY_MACRO(t)
vl_api_tunnel_mode_t mode
Definition: gre.api:48
u16 rxq_size[default=0]
Definition: af_xdp.api:49
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
void af_xdp_create_if(vlib_main_t *vm, af_xdp_create_if_args_t *args)
Definition: device.c:305
static void setup_message_id_table(api_main_t *am)
Definition: api.c:802
static void vl_api_af_xdp_delete_t_handler(vl_api_af_xdp_delete_t *mp)
Definition: api.c:78
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u16 txq_size[default=0]
Definition: af_xdp.api:50
vl_api_af_xdp_mode_t mode[default=0]
Definition: af_xdp.api:51
u16 rxq_num[default=1]
Definition: af_xdp.api:48
string prog[256]
Definition: af_xdp.api:52
VLIB_API_INIT_FUNCTION(af_xdp_plugin_api_hookup)
af_xdp_mode_t mode
Definition: af_xdp.h:133