avf: binary API and configurable RX/TX queue size
[vpp.git] / src / plugins / avf / avf_api.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 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 <avf/avf.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27
28 /* define message IDs */
29 #include <avf/avf_msg_enum.h>
30
31 /* define message structures */
32 #define vl_typedefs
33 #include <avf/avf_all_api_h.h>
34 #undef vl_typedefs
35
36 /* define generated endian-swappers */
37 #define vl_endianfun
38 #include <avf/avf_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define vl_printfun
44 #include <avf/avf_all_api_h.h>
45 #undef vl_printfun
46
47 /* get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <avf/avf_all_api_h.h>
50 #undef vl_api_version
51
52 #include <vlibapi/api_helper_macros.h>
53
54 #define foreach_avf_plugin_api_msg      \
55 _(AVF_CREATE, avf_create)               \
56 _(AVF_DELETE, avf_delete)
57
58 static void
59 vl_api_avf_create_t_handler (vl_api_avf_create_t * mp)
60 {
61   vlib_main_t *vm = vlib_get_main ();
62   avf_main_t *am = &avf_main;
63   vl_api_avf_create_reply_t *rmp;
64   avf_create_if_args_t args;
65   int rv = 0;
66
67   memset (&args, 0, sizeof (avf_create_if_args_t));
68
69   args.enable_elog = ntohl (mp->enable_elog);
70   args.addr.as_u32 = ntohl (mp->pci_addr);
71   args.rxq_size = ntohl (mp->rxq_size);
72   args.txq_size = ntohl (mp->txq_size);
73
74   avf_create_if (vm, &args);
75
76   rv = args.rv;
77
78   REPLY_MACRO (VL_API_AVF_CREATE_REPLY + am->msg_id_base);
79 }
80
81 static void
82 vl_api_avf_delete_t_handler (vl_api_avf_delete_t * mp)
83 {
84   vlib_main_t *vm = vlib_get_main ();
85   vnet_main_t *vnm = vnet_get_main ();
86   avf_main_t *am = &avf_main;
87   vl_api_avf_delete_reply_t *rmp;
88   avf_device_t *ad;
89   vnet_hw_interface_t *hw;
90   int rv = 0;
91
92   hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
93   if (hw == NULL || avf_device_class.index != hw->dev_class_index)
94     {
95       rv = VNET_API_ERROR_INVALID_INTERFACE;
96       goto reply;
97     }
98
99   ad = pool_elt_at_index (am->devices, hw->dev_instance);
100
101   avf_delete_if (vm, ad);
102
103 reply:
104   REPLY_MACRO (VL_API_AVF_DELETE_REPLY + am->msg_id_base);
105 }
106
107 #define vl_msg_name_crc_list
108 #include <avf/avf_all_api_h.h>
109 #undef vl_msg_name_crc_list
110
111 static void
112 setup_message_id_table (avf_main_t * avm, api_main_t * am)
113 {
114 #define _(id,n,crc) \
115   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + avm->msg_id_base);
116   foreach_vl_msg_name_crc_avf;
117 #undef _
118 }
119
120 /* set tup the API message handling tables */
121 static clib_error_t *
122 avf_plugin_api_hookup (vlib_main_t * vm)
123 {
124   avf_main_t *avm = &avf_main;
125   api_main_t *am = &api_main;
126   u8 *name;
127
128   /* construct the API name */
129   name = format (0, "avf_%08x%c", api_version, 0);
130
131   /* ask for a correctly-sized block of API message decode slots */
132   avm->msg_id_base = vl_msg_api_get_msg_ids
133     ((char *) name, VL_MSG_FIRST_AVAILABLE);
134
135 #define _(N,n)                                                  \
136     vl_msg_api_set_handlers((VL_API_##N + avm->msg_id_base),    \
137                            #n,                                  \
138                            vl_api_##n##_t_handler,              \
139                            vl_noop_handler,                     \
140                            vl_api_##n##_t_endian,               \
141                            vl_api_##n##_t_print,                \
142                            sizeof(vl_api_##n##_t), 1);
143   foreach_avf_plugin_api_msg;
144 #undef _
145
146   /* set up the (msg_name, crc, message-id) table */
147   setup_message_id_table (avm, am);
148
149   vec_free (name);
150   return 0;
151 }
152
153 VLIB_API_INIT_FUNCTION (avf_plugin_api_hookup);
154
155 /*
156  * fd.io coding-style-patch-verification: ON
157  *
158  * Local Variables:
159  * eval: (c-set-style "gnu")
160  * End:
161  */