45dab284e4c2cd7770b5ef837a742c7e9c30ba55
[vpp.git] / src / plugins / af_xdp / 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 <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
30 #include <vlibapi/api_helper_macros.h>
31
32 static af_xdp_mode_t
33 af_xdp_api_mode (vl_api_af_xdp_mode_t mode)
34 {
35   switch (mode)
36     {
37     case AF_XDP_API_MODE_AUTO:
38       return AF_XDP_MODE_AUTO;
39     case AF_XDP_API_MODE_COPY:
40       return AF_XDP_MODE_COPY;
41     case AF_XDP_API_MODE_ZERO_COPY:
42       return AF_XDP_MODE_ZERO_COPY;
43     }
44   return AF_XDP_MODE_AUTO;
45 }
46
47 static af_xdp_create_flag_t
48 af_xdp_api_flags (vl_api_af_xdp_flag_t flags)
49 {
50   af_xdp_create_flag_t cflags = 0;
51
52   if (flags & AF_XDP_API_FLAGS_NO_SYSCALL_LOCK)
53     cflags |= AF_XDP_CREATE_FLAGS_NO_SYSCALL_LOCK;
54
55   return cflags;
56 }
57
58 static void
59 vl_api_af_xdp_create_t_handler (vl_api_af_xdp_create_t * mp)
60 {
61   vlib_main_t *vm = vlib_get_main ();
62   af_xdp_main_t *rm = &af_xdp_main;
63   vl_api_af_xdp_create_reply_t *rmp;
64   af_xdp_create_if_args_t args;
65   int rv;
66
67   clib_memset (&args, 0, sizeof (af_xdp_create_if_args_t));
68
69   args.linux_ifname = mp->host_if[0] ? (char *) mp->host_if : 0;
70   args.name = mp->name[0] ? (char *) mp->name : 0;
71   args.prog = mp->prog[0] ? (char *) mp->prog : 0;
72   args.mode = af_xdp_api_mode (mp->mode);
73   args.flags = af_xdp_api_flags (mp->flags);
74   args.rxq_size = ntohs (mp->rxq_size);
75   args.txq_size = ntohs (mp->txq_size);
76   args.rxq_num = ntohs (mp->rxq_num);
77
78   af_xdp_create_if (vm, &args);
79   rv = args.rv;
80
81   /* *INDENT-OFF* */
82   REPLY_MACRO2 (VL_API_AF_XDP_CREATE_REPLY + rm->msg_id_base,
83     ({
84       rmp->sw_if_index = ntohl (args.sw_if_index);
85     }));
86   /* *INDENT-ON* */
87 }
88
89 static void
90 vl_api_af_xdp_create_v2_t_handler (vl_api_af_xdp_create_v2_t *mp)
91 {
92   vlib_main_t *vm = vlib_get_main ();
93   af_xdp_main_t *rm = &af_xdp_main;
94   vl_api_af_xdp_create_v2_reply_t *rmp;
95   af_xdp_create_if_args_t args;
96   int rv;
97
98   clib_memset (&args, 0, sizeof (af_xdp_create_if_args_t));
99
100   args.linux_ifname = mp->host_if[0] ? (char *) mp->host_if : 0;
101   args.name = mp->name[0] ? (char *) mp->name : 0;
102   args.prog = mp->prog[0] ? (char *) mp->prog : 0;
103   args.netns = mp->namespace[0] ? (char *) mp->namespace : 0;
104   args.mode = af_xdp_api_mode (mp->mode);
105   args.flags = af_xdp_api_flags (mp->flags);
106   args.rxq_size = ntohs (mp->rxq_size);
107   args.txq_size = ntohs (mp->txq_size);
108   args.rxq_num = ntohs (mp->rxq_num);
109
110   af_xdp_create_if (vm, &args);
111   rv = args.rv;
112
113   /* clang-format off */
114   REPLY_MACRO2 (VL_API_AF_XDP_CREATE_V2_REPLY + rm->msg_id_base,
115     ({
116       rmp->sw_if_index = ntohl (args.sw_if_index);
117     }));
118   /* clang-format on */
119 }
120
121 static void
122 vl_api_af_xdp_delete_t_handler (vl_api_af_xdp_delete_t * mp)
123 {
124   vlib_main_t *vm = vlib_get_main ();
125   vnet_main_t *vnm = vnet_get_main ();
126   af_xdp_main_t *rm = &af_xdp_main;
127   vl_api_af_xdp_delete_reply_t *rmp;
128   af_xdp_device_t *rd;
129   vnet_hw_interface_t *hw;
130   int rv = 0;
131
132   hw =
133     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
134                                                    htonl (mp->sw_if_index));
135   if (hw == NULL || af_xdp_device_class.index != hw->dev_class_index)
136     {
137       rv = VNET_API_ERROR_INVALID_INTERFACE;
138       goto reply;
139     }
140
141   rd = pool_elt_at_index (rm->devices, hw->dev_instance);
142
143   af_xdp_delete_if (vm, rd);
144
145 reply:
146   REPLY_MACRO (VL_API_AF_XDP_DELETE_REPLY + rm->msg_id_base);
147 }
148
149 /* set tup the API message handling tables */
150 #include <af_xdp/af_xdp.api.c>
151 static clib_error_t *
152 af_xdp_plugin_api_hookup (vlib_main_t * vm)
153 {
154   af_xdp_main_t *rm = &af_xdp_main;
155
156   /* ask for a correctly-sized block of API message decode slots */
157   rm->msg_id_base = setup_message_id_table ();
158   return 0;
159 }
160
161 VLIB_API_INIT_FUNCTION (af_xdp_plugin_api_hookup);
162
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "gnu")
168  * End:
169  */