af_xdp: introduce to netns api
[vpp.git] / src / plugins / af_xdp / test_api.c
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 <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
22
23 #include <vat/vat.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26
27 #include <vppinfra/error.h>
28 #include <af_xdp/af_xdp.h>
29
30 #define __plugin_msg_base af_xdp_test_main.msg_id_base
31 #include <vlibapi/vat_helper_macros.h>
32
33 /* declare message IDs */
34 #include <af_xdp/af_xdp.api_enum.h>
35 #include <af_xdp/af_xdp.api_types.h>
36
37 typedef struct
38 {
39   /* API message ID base */
40   u16 msg_id_base;
41   vat_main_t *vat_main;
42 } af_xdp_test_main_t;
43
44 af_xdp_test_main_t af_xdp_test_main;
45
46 static vl_api_af_xdp_mode_t
47 api_af_xdp_mode (af_xdp_mode_t mode)
48 {
49   switch (mode)
50     {
51     case AF_XDP_MODE_AUTO:
52       return AF_XDP_API_MODE_AUTO;
53     case AF_XDP_MODE_COPY:
54       return AF_XDP_API_MODE_COPY;
55     case AF_XDP_MODE_ZERO_COPY:
56       return AF_XDP_API_MODE_ZERO_COPY;
57     }
58   return ~0;
59 }
60
61 /* af_xdp create API */
62 static int
63 api_af_xdp_create (vat_main_t * vam)
64 {
65   vl_api_af_xdp_create_t *mp;
66   af_xdp_create_if_args_t args;
67   int ret;
68
69   if (!unformat_user (vam->input, unformat_af_xdp_create_if_args, &args))
70     {
71       clib_warning ("unknown input `%U'", format_unformat_error, vam->input);
72       return -99;
73     }
74
75   M (AF_XDP_CREATE, mp);
76
77   snprintf ((char *) mp->host_if, sizeof (mp->host_if), "%s",
78             args.linux_ifname ? : "");
79   snprintf ((char *) mp->name, sizeof (mp->name), "%s", args.name ? : "");
80   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
81   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
82   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
83   mp->mode = api_af_xdp_mode (args.mode);
84   if (args.flags & AF_XDP_CREATE_FLAGS_NO_SYSCALL_LOCK)
85     mp->flags |= AF_XDP_API_FLAGS_NO_SYSCALL_LOCK;
86   snprintf ((char *) mp->prog, sizeof (mp->prog), "%s", args.prog ? : "");
87
88   S (mp);
89   W (ret);
90
91   return ret;
92 }
93
94 /* af_xdp create v2 API */
95 static int
96 api_af_xdp_create_v2 (vat_main_t *vam)
97 {
98   vl_api_af_xdp_create_v2_t *mp;
99   af_xdp_create_if_args_t args;
100   int ret;
101
102   if (!unformat_user (vam->input, unformat_af_xdp_create_if_args, &args))
103     {
104       clib_warning ("unknown input `%U'", format_unformat_error, vam->input);
105       return -99;
106     }
107
108   M (AF_XDP_CREATE, mp);
109
110   snprintf ((char *) mp->host_if, sizeof (mp->host_if), "%s",
111             args.linux_ifname ?: "");
112   snprintf ((char *) mp->name, sizeof (mp->name), "%s", args.name ?: "");
113   snprintf ((char *) mp->namespace, sizeof (mp->namespace), "%s",
114             args.netns ?: "");
115   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
116   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
117   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
118   mp->mode = api_af_xdp_mode (args.mode);
119   if (args.flags & AF_XDP_CREATE_FLAGS_NO_SYSCALL_LOCK)
120     mp->flags |= AF_XDP_API_FLAGS_NO_SYSCALL_LOCK;
121   snprintf ((char *) mp->prog, sizeof (mp->prog), "%s", args.prog ?: "");
122
123   S (mp);
124   W (ret);
125
126   return ret;
127 }
128
129 /* af_xdp-create reply handler */
130 static void
131 vl_api_af_xdp_create_reply_t_handler (vl_api_af_xdp_create_reply_t * mp)
132 {
133   vat_main_t *vam = af_xdp_test_main.vat_main;
134   i32 retval = ntohl (mp->retval);
135
136   if (retval == 0)
137     {
138       fformat (vam->ofp, "created af_xdp with sw_if_index %d\n",
139                ntohl (mp->sw_if_index));
140     }
141
142   vam->retval = retval;
143   vam->result_ready = 1;
144   vam->regenerate_interface_table = 1;
145 }
146
147 /* af_xdp-create v2 reply handler */
148 static void
149 vl_api_af_xdp_create_v2_reply_t_handler (vl_api_af_xdp_create_v2_reply_t *mp)
150 {
151   vat_main_t *vam = af_xdp_test_main.vat_main;
152   i32 retval = ntohl (mp->retval);
153
154   if (retval == 0)
155     {
156       fformat (vam->ofp, "created af_xdp with sw_if_index %d\n",
157                ntohl (mp->sw_if_index));
158     }
159
160   vam->retval = retval;
161   vam->result_ready = 1;
162   vam->regenerate_interface_table = 1;
163 }
164
165 /* af_xdp delete API */
166 static int
167 api_af_xdp_delete (vat_main_t * vam)
168 {
169   unformat_input_t *i = vam->input;
170   vl_api_af_xdp_delete_t *mp;
171   u32 sw_if_index = 0;
172   u8 index_defined = 0;
173   int ret;
174
175   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
176     {
177       if (unformat (i, "sw_if_index %u", &sw_if_index))
178         index_defined = 1;
179       else
180         {
181           clib_warning ("unknown input '%U'", format_unformat_error, i);
182           return -99;
183         }
184     }
185
186   if (!index_defined)
187     {
188       errmsg ("missing sw_if_index\n");
189       return -99;
190     }
191
192   M (AF_XDP_DELETE, mp);
193
194   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
195
196   S (mp);
197   W (ret);
198
199   return ret;
200 }
201
202 #include <af_xdp/af_xdp.api_test.c>
203
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "gnu")
209  * End:
210  */