rdma: add support for RSS configuration
[vpp.git] / src / plugins / rdma / api.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2019 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 <rdma/rdma.h>
22
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25
26 /* define message IDs */
27 #include <rdma/rdma.api_enum.h>
28 #include <rdma/rdma.api_types.h>
29
30 #include <vlibapi/api_helper_macros.h>
31
32 static rdma_mode_t
33 rdma_api_mode (vl_api_rdma_mode_t mode)
34 {
35   switch (mode)
36     {
37     case RDMA_API_MODE_AUTO:
38       return RDMA_MODE_AUTO;
39     case RDMA_API_MODE_IBV:
40       return RDMA_MODE_IBV;
41     case RDMA_API_MODE_DV:
42       return RDMA_MODE_DV;
43     }
44   return RDMA_MODE_AUTO;
45 }
46
47 static rdma_rss4_t
48 rdma_api_rss4 (const vl_api_rdma_rss4_t rss4)
49 {
50   switch (rss4)
51     {
52     case RDMA_API_RSS4_AUTO:
53       return RDMA_RSS4_AUTO;
54     case RDMA_API_RSS4_IP:
55       return RDMA_RSS4_IP;
56     case RDMA_API_RSS4_IP_UDP:
57       return RDMA_RSS4_IP_UDP;
58     case RDMA_API_RSS4_IP_TCP:
59       return RDMA_RSS4_IP_TCP;
60     }
61   return RDMA_RSS4_AUTO;
62 }
63
64 static rdma_rss6_t
65 rdma_api_rss6 (const vl_api_rdma_rss6_t rss6)
66 {
67   switch (rss6)
68     {
69     case RDMA_API_RSS6_AUTO:
70       return RDMA_RSS6_AUTO;
71     case RDMA_API_RSS6_IP:
72       return RDMA_RSS6_IP;
73     case RDMA_API_RSS6_IP_UDP:
74       return RDMA_RSS6_IP_UDP;
75     case RDMA_API_RSS6_IP_TCP:
76       return RDMA_RSS6_IP_TCP;
77     }
78   return RDMA_RSS6_AUTO;
79 }
80
81 static void
82 vl_api_rdma_create_v3_t_handler (vl_api_rdma_create_v3_t *mp)
83 {
84   vlib_main_t *vm = vlib_get_main ();
85   rdma_main_t *rm = &rdma_main;
86   vl_api_rdma_create_v3_reply_t *rmp;
87   rdma_create_if_args_t args;
88   int rv;
89
90   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
91
92   args.ifname = mp->host_if;
93   args.name = mp->name;
94   args.rxq_num = ntohs (mp->rxq_num);
95   args.rxq_size = ntohs (mp->rxq_size);
96   args.txq_size = ntohs (mp->txq_size);
97   args.mode = rdma_api_mode (mp->mode);
98   args.disable_striding_rq = 0;
99   args.no_multi_seg = mp->no_multi_seg;
100   args.max_pktlen = ntohs (mp->max_pktlen);
101   args.rss4 = rdma_api_rss4 (mp->rss4);
102   args.rss6 = rdma_api_rss6 (mp->rss6);
103   rdma_create_if (vm, &args);
104   rv = args.rv;
105
106   REPLY_MACRO2 (VL_API_RDMA_CREATE_V3_REPLY + rm->msg_id_base,
107                 ({ rmp->sw_if_index = ntohl (args.sw_if_index); }));
108 }
109
110 static void
111 vl_api_rdma_create_v2_t_handler (vl_api_rdma_create_v2_t * mp)
112 {
113   vlib_main_t *vm = vlib_get_main ();
114   rdma_main_t *rm = &rdma_main;
115   vl_api_rdma_create_v2_reply_t *rmp;
116   rdma_create_if_args_t args;
117   int rv;
118
119   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
120
121   args.ifname = mp->host_if;
122   args.name = mp->name;
123   args.rxq_num = ntohs (mp->rxq_num);
124   args.rxq_size = ntohs (mp->rxq_size);
125   args.txq_size = ntohs (mp->txq_size);
126   args.mode = rdma_api_mode (mp->mode);
127   args.disable_striding_rq = 0;
128   args.no_multi_seg = mp->no_multi_seg;
129   args.max_pktlen = ntohs (mp->max_pktlen);
130   rdma_create_if (vm, &args);
131   rv = args.rv;
132
133   /* *INDENT-OFF* */
134   REPLY_MACRO2 (VL_API_RDMA_CREATE_V2_REPLY + rm->msg_id_base,
135     ({
136       rmp->sw_if_index = ntohl (args.sw_if_index);
137     }));
138   /* *INDENT-ON* */
139 }
140
141 static void
142 vl_api_rdma_create_t_handler (vl_api_rdma_create_t * mp)
143 {
144   vlib_main_t *vm = vlib_get_main ();
145   rdma_main_t *rm = &rdma_main;
146   vl_api_rdma_create_reply_t *rmp;
147   rdma_create_if_args_t args;
148   int rv;
149
150   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
151
152   args.ifname = mp->host_if;
153   args.name = mp->name;
154   args.rxq_num = ntohs (mp->rxq_num);
155   args.rxq_size = ntohs (mp->rxq_size);
156   args.txq_size = ntohs (mp->txq_size);
157   args.mode = rdma_api_mode (mp->mode);
158   args.disable_striding_rq = 0;
159   args.no_multi_seg = 1;
160   args.max_pktlen = 0;
161
162   rdma_create_if (vm, &args);
163   rv = args.rv;
164
165   /* *INDENT-OFF* */
166   REPLY_MACRO2 (VL_API_RDMA_CREATE_REPLY + rm->msg_id_base,
167     ({
168       rmp->sw_if_index = ntohl (args.sw_if_index);
169     }));
170   /* *INDENT-ON* */
171 }
172
173 static void
174 vl_api_rdma_delete_t_handler (vl_api_rdma_delete_t * mp)
175 {
176   vlib_main_t *vm = vlib_get_main ();
177   vnet_main_t *vnm = vnet_get_main ();
178   rdma_main_t *rm = &rdma_main;
179   vl_api_rdma_delete_reply_t *rmp;
180   rdma_device_t *rd;
181   vnet_hw_interface_t *hw;
182   int rv = 0;
183
184   hw =
185     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
186                                                    htonl (mp->sw_if_index));
187   if (hw == NULL || rdma_device_class.index != hw->dev_class_index)
188     {
189       rv = VNET_API_ERROR_INVALID_INTERFACE;
190       goto reply;
191     }
192
193   rd = pool_elt_at_index (rm->devices, hw->dev_instance);
194
195   rdma_delete_if (vm, rd);
196
197 reply:
198   REPLY_MACRO (VL_API_RDMA_DELETE_REPLY + rm->msg_id_base);
199 }
200
201 /* set tup the API message handling tables */
202 #include <rdma/rdma.api.c>
203 static clib_error_t *
204 rdma_plugin_api_hookup (vlib_main_t * vm)
205 {
206   rdma_main_t *rm = &rdma_main;
207
208   /* ask for a correctly-sized block of API message decode slots */
209   rm->msg_id_base = setup_message_id_table ();
210   return 0;
211 }
212
213 VLIB_API_INIT_FUNCTION (rdma_plugin_api_hookup);
214
215 /*
216  * fd.io coding-style-patch-verification: ON
217  *
218  * Local Variables:
219  * eval: (c-set-style "gnu")
220  * End:
221  */