rdma: add rdma API
[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 /* rdma create API */
47 static int
48 api_rdma_create (vat_main_t * vam)
49 {
50   vl_api_rdma_create_t *mp;
51   rdma_create_if_args_t args;
52   int ret;
53
54   if (!unformat_user (vam->input, unformat_rdma_create_if_args, &args))
55     {
56       clib_warning ("unknown input `%U'", format_unformat_error, vam->input);
57       return -99;
58     }
59
60   M (RDMA_CREATE, mp);
61
62   snprintf ((char *) mp->host_if, sizeof (mp->host_if), "%s", args.ifname);
63   snprintf ((char *) mp->name, sizeof (mp->name), "%s", args.name);
64   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
65   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
66   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
67
68   S (mp);
69   W (ret);
70
71   return ret;
72 }
73
74 /* rdma-create reply handler */
75 static void
76 vl_api_rdma_create_reply_t_handler (vl_api_rdma_create_reply_t * mp)
77 {
78   vat_main_t *vam = rdma_test_main.vat_main;
79   i32 retval = ntohl (mp->retval);
80
81   if (retval == 0)
82     {
83       fformat (vam->ofp, "created rdma with sw_if_index %d\n",
84                ntohl (mp->sw_if_index));
85     }
86
87   vam->retval = retval;
88   vam->result_ready = 1;
89   vam->regenerate_interface_table = 1;
90 }
91
92 /* rdma delete API */
93 static int
94 api_rdma_delete (vat_main_t * vam)
95 {
96   unformat_input_t *i = vam->input;
97   vl_api_rdma_delete_t *mp;
98   u32 sw_if_index = 0;
99   u8 index_defined = 0;
100   int ret;
101
102   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (i, "sw_if_index %u", &sw_if_index))
105         index_defined = 1;
106       else
107         {
108           clib_warning ("unknown input '%U'", format_unformat_error, i);
109           return -99;
110         }
111     }
112
113   if (!index_defined)
114     {
115       errmsg ("missing sw_if_index\n");
116       return -99;
117     }
118
119   M (RDMA_DELETE, mp);
120
121   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
122
123   S (mp);
124   W (ret);
125
126   return ret;
127 }
128
129 #include <rdma/rdma.api_test.c>
130
131 /*
132  * fd.io coding-style-patch-verification: ON
133  *
134  * Local Variables:
135  * eval: (c-set-style "gnu")
136  * End:
137  */