idpf: add native idpf driver plugin
[vpp.git] / src / plugins / idpf / idpf_test.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2023 Intel 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 <idpf/idpf.h>
29
30 #define __plugin_msg_base idpf_test_main.msg_id_base
31 #include <vlibapi/vat_helper_macros.h>
32
33 /* declare message IDs */
34 #include <idpf/idpf.api_enum.h>
35 #include <idpf/idpf.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 } idpf_test_main_t;
43
44 idpf_test_main_t idpf_test_main;
45
46 /* idpf create API */
47 static int
48 api_idpf_create (vat_main_t *vam)
49 {
50   unformat_input_t *i = vam->input;
51   vl_api_idpf_create_t *mp;
52   idpf_create_if_args_t args;
53   uint32_t tmp;
54   int ret;
55   u32 x[4];
56
57   clib_memset (&args, 0, sizeof (idpf_create_if_args_t));
58
59   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
60     {
61       if (unformat (i, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
62         {
63           args.addr.domain = x[0];
64           args.addr.bus = x[1];
65           args.addr.slot = x[2];
66           args.addr.function = x[3];
67         }
68       else if (unformat (i, "rx-single %u", &tmp))
69         args.rxq_single = 1;
70       else if (unformat (i, "tx-single %u", &tmp))
71         args.txq_single = 1;
72       else if (unformat (i, "rxq-size %u", &tmp))
73         args.rxq_size = tmp;
74       else if (unformat (i, "txq-size %u", &tmp))
75         args.txq_size = tmp;
76       else if (unformat (i, "rxq-num %u", &tmp))
77         args.rxq_num = tmp;
78       else if (unformat (i, "txq-num %u", &tmp))
79         args.txq_num = tmp;
80       else if (unformat (i, "vport-num %u", &tmp))
81         args.req_vport_nb = tmp;
82       else
83         {
84           clib_warning ("unknown input '%U'", format_unformat_error, i);
85           return -99;
86         }
87     }
88
89   M (IDPF_CREATE, mp);
90
91   mp->pci_addr = clib_host_to_net_u32 (args.addr.as_u32);
92   mp->rxq_single = clib_host_to_net_u16 (args.rxq_single);
93   mp->txq_single = clib_host_to_net_u16 (args.txq_single);
94   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
95   mp->txq_num = clib_host_to_net_u16 (args.txq_num);
96   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
97   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
98   mp->req_vport_nb = clib_host_to_net_u16 (args.req_vport_nb);
99
100   S (mp);
101   W (ret);
102
103   return ret;
104 }
105
106 /* idpf-create reply handler */
107 static void
108 vl_api_idpf_create_reply_t_handler (vl_api_idpf_create_reply_t *mp)
109 {
110   vat_main_t *vam = idpf_test_main.vat_main;
111   i32 retval = ntohl (mp->retval);
112
113   if (retval == 0)
114     {
115       fformat (vam->ofp, "created idpf with sw_if_index %d\n",
116                ntohl (mp->sw_if_index));
117     }
118
119   vam->retval = retval;
120   vam->result_ready = 1;
121   vam->regenerate_interface_table = 1;
122 }
123
124 /* idpf delete API */
125 static int
126 api_idpf_delete (vat_main_t *vam)
127 {
128   unformat_input_t *i = vam->input;
129   vl_api_idpf_delete_t *mp;
130   u32 sw_if_index = 0;
131   u8 index_defined = 0;
132   int ret;
133
134   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
135     {
136       if (unformat (i, "sw_if_index %u", &sw_if_index))
137         index_defined = 1;
138       else
139         {
140           clib_warning ("unknown input '%U'", format_unformat_error, i);
141           return -99;
142         }
143     }
144
145   if (!index_defined)
146     {
147       errmsg ("missing sw_if_index\n");
148       return -99;
149     }
150
151   M (IDPF_DELETE, mp);
152
153   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
154
155   S (mp);
156   W (ret);
157
158   return ret;
159 }
160
161 #include <idpf/idpf.api_test.c>
162
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "gnu")
168  * End:
169  */