ff5dec1ad95e60ff6d976044f9cb0e20e4aa4553
[vpp.git] / src / plugins / rdma / test_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 <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 <rdma/rdma.h>
29
30 #define __plugin_msg_base rdma_test_main.msg_id_base
31 #include <vlibapi/vat_helper_macros.h>
32
33 /* declare message IDs */
34 #include <rdma/rdma.api_enum.h>
35 #include <rdma/rdma.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 } rdma_test_main_t;
43
44 rdma_test_main_t rdma_test_main;
45
46 static vl_api_rdma_mode_t
47 api_rdma_mode (rdma_mode_t mode)
48 {
49   switch (mode)
50     {
51     case RDMA_MODE_AUTO:
52       return RDMA_API_MODE_AUTO;
53     case RDMA_MODE_IBV:
54       return RDMA_API_MODE_IBV;
55     case RDMA_MODE_DV:
56       return RDMA_API_MODE_DV;
57     }
58   return ~0;
59 }
60
61 /* rdma create API */
62 static int
63 api_rdma_create (vat_main_t * vam)
64 {
65   vl_api_rdma_create_t *mp;
66   rdma_create_if_args_t args;
67   int ret;
68
69   if (!unformat_user (vam->input, unformat_rdma_create_if_args, &args))
70     {
71       clib_warning ("unknown input `%U'", format_unformat_error, vam->input);
72       return -99;
73     }
74
75   M (RDMA_CREATE, mp);
76
77   snprintf ((char *) mp->host_if, sizeof (mp->host_if), "%s", args.ifname);
78   snprintf ((char *) mp->name, sizeof (mp->name), "%s", args.name);
79   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
80   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
81   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
82   mp->mode = api_rdma_mode (args.mode);
83
84   S (mp);
85   W (ret);
86
87   return ret;
88 }
89
90 static int
91 api_rdma_create_v2 (vat_main_t * vam)
92 {
93   vl_api_rdma_create_v2_t *mp;
94   rdma_create_if_args_t args;
95   int ret;
96
97   if (!unformat_user (vam->input, unformat_rdma_create_if_args, &args))
98     {
99       clib_warning ("unknown input `%U'", format_unformat_error, vam->input);
100       return -99;
101     }
102
103   M (RDMA_CREATE_V2, mp);
104
105   snprintf ((char *) mp->host_if, sizeof (mp->host_if), "%s", args.ifname);
106   snprintf ((char *) mp->name, sizeof (mp->name), "%s", args.name);
107   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
108   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
109   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
110   mp->mode = api_rdma_mode (args.mode);
111   mp->no_multi_seg = args.no_multi_seg;
112   mp->max_pktlen = clib_host_to_net_u16 (args.max_pktlen);
113
114   S (mp);
115   W (ret);
116
117   return ret;
118 }
119
120 /* rdma-create reply handler */
121 static void
122 vl_api_rdma_create_reply_t_handler (vl_api_rdma_create_reply_t * mp)
123 {
124   vat_main_t *vam = rdma_test_main.vat_main;
125   i32 retval = ntohl (mp->retval);
126
127   if (retval == 0)
128     {
129       fformat (vam->ofp, "created rdma with sw_if_index %d\n",
130                ntohl (mp->sw_if_index));
131     }
132
133   vam->retval = retval;
134   vam->result_ready = 1;
135   vam->regenerate_interface_table = 1;
136 }
137
138 /* rdma-create reply handler */
139 static void
140 vl_api_rdma_create_v2_reply_t_handler (vl_api_rdma_create_v2_reply_t * mp)
141 {
142   vat_main_t *vam = rdma_test_main.vat_main;
143   i32 retval = ntohl (mp->retval);
144
145   if (retval == 0)
146     {
147       fformat (vam->ofp, "created rdma with sw_if_index %d\n",
148                ntohl (mp->sw_if_index));
149     }
150
151   vam->retval = retval;
152   vam->result_ready = 1;
153   vam->regenerate_interface_table = 1;
154 }
155
156 /* rdma delete API */
157 static int
158 api_rdma_delete (vat_main_t * vam)
159 {
160   unformat_input_t *i = vam->input;
161   vl_api_rdma_delete_t *mp;
162   u32 sw_if_index = 0;
163   u8 index_defined = 0;
164   int ret;
165
166   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
167     {
168       if (unformat (i, "sw_if_index %u", &sw_if_index))
169         index_defined = 1;
170       else
171         {
172           clib_warning ("unknown input '%U'", format_unformat_error, i);
173           return -99;
174         }
175     }
176
177   if (!index_defined)
178     {
179       errmsg ("missing sw_if_index\n");
180       return -99;
181     }
182
183   M (RDMA_DELETE, mp);
184
185   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
186
187   S (mp);
188   W (ret);
189
190   return ret;
191 }
192
193 #include <rdma/rdma.api_test.c>
194
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */