rdma: add rdma API
[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 void
33 vl_api_rdma_create_t_handler (vl_api_rdma_create_t * mp)
34 {
35   vlib_main_t *vm = vlib_get_main ();
36   rdma_main_t *rm = &rdma_main;
37   vl_api_rdma_create_reply_t *rmp;
38   rdma_create_if_args_t args;
39   int rv;
40
41   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
42
43   args.ifname = mp->host_if;
44   args.name = mp->name;
45   args.rxq_num = ntohs (mp->rxq_num);
46   args.rxq_size = ntohs (mp->rxq_size);
47   args.txq_size = ntohs (mp->txq_size);
48
49   rdma_create_if (vm, &args);
50   rv = args.rv;
51
52   /* *INDENT-OFF* */
53   REPLY_MACRO2 (VL_API_RDMA_CREATE_REPLY + rm->msg_id_base,
54     ({
55       rmp->sw_if_index = ntohl (args.sw_if_index);
56     }));
57   /* *INDENT-ON* */
58 }
59
60 static void
61 vl_api_rdma_delete_t_handler (vl_api_rdma_delete_t * mp)
62 {
63   vlib_main_t *vm = vlib_get_main ();
64   vnet_main_t *vnm = vnet_get_main ();
65   rdma_main_t *rm = &rdma_main;
66   vl_api_rdma_delete_reply_t *rmp;
67   rdma_device_t *rd;
68   vnet_hw_interface_t *hw;
69   int rv = 0;
70
71   hw =
72     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
73                                                    htonl (mp->sw_if_index));
74   if (hw == NULL || rdma_device_class.index != hw->dev_class_index)
75     {
76       rv = VNET_API_ERROR_INVALID_INTERFACE;
77       goto reply;
78     }
79
80   rd = pool_elt_at_index (rm->devices, hw->dev_instance);
81
82   rdma_delete_if (vm, rd);
83
84 reply:
85   REPLY_MACRO (VL_API_RDMA_DELETE_REPLY + rm->msg_id_base);
86 }
87
88 /* set tup the API message handling tables */
89 #include <rdma/rdma.api.c>
90 static clib_error_t *
91 rdma_plugin_api_hookup (vlib_main_t * vm)
92 {
93   rdma_main_t *rm = &rdma_main;
94
95   /* ask for a correctly-sized block of API message decode slots */
96   rm->msg_id_base = setup_message_id_table ();
97   return 0;
98 }
99
100 VLIB_API_INIT_FUNCTION (rdma_plugin_api_hookup);
101
102 /*
103  * fd.io coding-style-patch-verification: ON
104  *
105  * Local Variables:
106  * eval: (c-set-style "gnu")
107  * End:
108  */