06e7385136f66cf149b91371784bbf183cbbb22a
[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 void
48 vl_api_rdma_create_v2_t_handler (vl_api_rdma_create_v2_t * mp)
49 {
50   vlib_main_t *vm = vlib_get_main ();
51   rdma_main_t *rm = &rdma_main;
52   vl_api_rdma_create_v2_reply_t *rmp;
53   rdma_create_if_args_t args;
54   int rv;
55
56   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
57
58   args.ifname = mp->host_if;
59   args.name = mp->name;
60   args.rxq_num = ntohs (mp->rxq_num);
61   args.rxq_size = ntohs (mp->rxq_size);
62   args.txq_size = ntohs (mp->txq_size);
63   args.mode = rdma_api_mode (mp->mode);
64   args.disable_striding_rq = 0;
65   args.no_multi_seg = mp->no_multi_seg;
66   args.max_pktlen = ntohs (mp->max_pktlen);
67   rdma_create_if (vm, &args);
68   rv = args.rv;
69
70   /* *INDENT-OFF* */
71   REPLY_MACRO2 (VL_API_RDMA_CREATE_V2_REPLY + rm->msg_id_base,
72     ({
73       rmp->sw_if_index = ntohl (args.sw_if_index);
74     }));
75   /* *INDENT-ON* */
76 }
77
78 static void
79 vl_api_rdma_create_t_handler (vl_api_rdma_create_t * mp)
80 {
81   vlib_main_t *vm = vlib_get_main ();
82   rdma_main_t *rm = &rdma_main;
83   vl_api_rdma_create_reply_t *rmp;
84   rdma_create_if_args_t args;
85   int rv;
86
87   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
88
89   args.ifname = mp->host_if;
90   args.name = mp->name;
91   args.rxq_num = ntohs (mp->rxq_num);
92   args.rxq_size = ntohs (mp->rxq_size);
93   args.txq_size = ntohs (mp->txq_size);
94   args.mode = rdma_api_mode (mp->mode);
95   args.disable_striding_rq = 0;
96   args.no_multi_seg = 1;
97   args.max_pktlen = 0;
98
99   rdma_create_if (vm, &args);
100   rv = args.rv;
101
102   /* *INDENT-OFF* */
103   REPLY_MACRO2 (VL_API_RDMA_CREATE_REPLY + rm->msg_id_base,
104     ({
105       rmp->sw_if_index = ntohl (args.sw_if_index);
106     }));
107   /* *INDENT-ON* */
108 }
109
110 static void
111 vl_api_rdma_delete_t_handler (vl_api_rdma_delete_t * mp)
112 {
113   vlib_main_t *vm = vlib_get_main ();
114   vnet_main_t *vnm = vnet_get_main ();
115   rdma_main_t *rm = &rdma_main;
116   vl_api_rdma_delete_reply_t *rmp;
117   rdma_device_t *rd;
118   vnet_hw_interface_t *hw;
119   int rv = 0;
120
121   hw =
122     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
123                                                    htonl (mp->sw_if_index));
124   if (hw == NULL || rdma_device_class.index != hw->dev_class_index)
125     {
126       rv = VNET_API_ERROR_INVALID_INTERFACE;
127       goto reply;
128     }
129
130   rd = pool_elt_at_index (rm->devices, hw->dev_instance);
131
132   rdma_delete_if (vm, rd);
133
134 reply:
135   REPLY_MACRO (VL_API_RDMA_DELETE_REPLY + rm->msg_id_base);
136 }
137
138 /* set tup the API message handling tables */
139 #include <rdma/rdma.api.c>
140 static clib_error_t *
141 rdma_plugin_api_hookup (vlib_main_t * vm)
142 {
143   rdma_main_t *rm = &rdma_main;
144
145   /* ask for a correctly-sized block of API message decode slots */
146   rm->msg_id_base = setup_message_id_table ();
147   return 0;
148 }
149
150 VLIB_API_INIT_FUNCTION (rdma_plugin_api_hookup);
151
152 /*
153  * fd.io coding-style-patch-verification: ON
154  *
155  * Local Variables:
156  * eval: (c-set-style "gnu")
157  * End:
158  */