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