FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
vmxnet3_api.c
Go to the documentation of this file.
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 <vmxnet3/vmxnet3.h>
24 
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 
28 /* define message IDs */
30 
31 /* define message structures */
32 #define vl_typedefs
34 #undef vl_typedefs
35 
36 /* define generated endian-swappers */
37 #define vl_endianfun
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
45 #undef vl_printfun
46 
47 /* get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
50 #undef vl_api_version
51 
53 
54 #define foreach_vmxnet3_plugin_api_msg \
55 _(VMXNET3_CREATE, vmxnet3_create) \
56 _(VMXNET3_DELETE, vmxnet3_delete) \
57 _(VMXNET3_DUMP, vmxnet3_dump)
58 
59 static void
61 {
66  int rv;
67 
68  memset (&args, 0, sizeof (vmxnet3_create_if_args_t));
69 
70  args.enable_elog = ntohl (mp->enable_elog);
71  args.addr.as_u32 = ntohl (mp->pci_addr);
72  args.rxq_size = ntohs (mp->rxq_size);
73  args.txq_size = ntohs (mp->txq_size);
74 
75  vmxnet3_create_if (vm, &args);
76  rv = args.rv;
77 
78  /* *INDENT-OFF* */
79  REPLY_MACRO2 (VL_API_VMXNET3_CREATE_REPLY + vmxm->msg_id_base,
80  ({
81  rmp->sw_if_index = ntohl (args.sw_if_index);
82  }));
83  /* *INDENT-ON* */
84 }
85 
86 static void
88 {
90  vnet_main_t *vnm = vnet_get_main ();
92  vl_api_vmxnet3_delete_reply_t *rmp;
93  vmxnet3_device_t *vd;
95  int rv = 0;
96 
97  hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
98  if (hw == NULL || vmxnet3_device_class.index != hw->dev_class_index)
99  {
100  rv = VNET_API_ERROR_INVALID_INTERFACE;
101  goto reply;
102  }
103 
104  vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
105 
106  vmxnet3_delete_if (vm, vd);
107 
108 reply:
109  REPLY_MACRO (VL_API_VMXNET3_DELETE_REPLY + vmxm->msg_id_base);
110 }
111 
112 static void
114  u16 rx_qid, vmxnet3_rxq_t * rxq, u16 tx_qid,
115  vmxnet3_txq_t * txq, vnet_sw_interface_t * swif,
116  u8 * interface_name, u32 context)
117 {
119  vnet_main_t *vnm = vnet_get_main ();
120  vmxnet3_main_t *vmxm = &vmxnet3_main;
121  vnet_hw_interface_t *hwif;
122  vmxnet3_rx_ring *ring;
123  u16 rid;
124 
125  hwif = vnet_get_sup_hw_interface (vnm, swif->sw_if_index);
126 
127  mp = vl_msg_api_alloc (sizeof (*mp));
128  memset (mp, 0, sizeof (*mp));
129 
130  mp->_vl_msg_id = htons (VL_API_VMXNET3_DETAILS + vmxm->msg_id_base);
131  mp->context = context;
132 
133  mp->sw_if_index = htonl (swif->sw_if_index);
134  strncpy ((char *) mp->if_name,
135  (char *) interface_name, ARRAY_LEN (mp->if_name) - 1);
136 
137  if (hwif->hw_address)
138  memcpy (mp->hw_addr, hwif->hw_address, ARRAY_LEN (mp->hw_addr));
139 
140  mp->version = vd->version;
141  mp->pci_addr = ntohl (vd->pci_addr.as_u32);
142 
143  mp->rx_qsize = htons (rxq->size);
144  mp->rx_next = htons (rxq->rx_comp_ring.next);
145  for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
146  {
147  ring = &rxq->rx_ring[rid];
148  mp->rx_fill[rid] = htons (ring->fill);
149  mp->rx_produce[rid] = htons (ring->produce);
150  mp->rx_consume[rid] = htons (ring->consume);
151  }
152  mp->tx_qsize = htons (txq->size);
153  mp->tx_next = htons (txq->tx_comp_ring.next);
154  mp->tx_produce = htons (txq->tx_ring.produce);
155  mp->tx_consume = htons (txq->tx_ring.consume);
156 
157  mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
158 
159  vl_api_send_msg (reg, (u8 *) mp);
160 }
161 
162 /**
163  * @brief Message handler for vmxnet3_dump API.
164  * @param mp vl_api_vmxnet3_dump_t * mp the api message
165  */
166 static void
168 {
169  vmxnet3_main_t *vmxm = &vmxnet3_main;
170  vnet_main_t *vnm = vnet_get_main ();
171  vnet_sw_interface_t *swif;
172  vmxnet3_device_t *vd;
173  u8 *if_name = 0;
175  vmxnet3_rxq_t *rxq;
176  vmxnet3_txq_t *txq;
177  u16 qid = 0;
178 
180  if (!reg)
181  return;
182 
183  /* *INDENT-OFF* */
184  pool_foreach (vd, vmxm->devices,
185  ({
186  swif = vnet_get_sw_interface (vnm, vd->sw_if_index);
187  if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm,
188  swif, 0);
189  rxq = vec_elt_at_index (vd->rxqs, qid);
190  txq = vec_elt_at_index (vd->txqs, qid);
191  send_vmxnet3_details (reg, vd, qid, rxq, qid, txq, swif, if_name,
192  mp->context);
193  _vec_len (if_name) = 0;
194  }));
195  /* *INDENT-ON* */
196 
197  vec_free (if_name);
198 }
199 
200 #define vl_msg_name_crc_list
202 #undef vl_msg_name_crc_list
203 
204 static void
206 {
207 #define _(id,n,crc) \
208  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base);
209  foreach_vl_msg_name_crc_vmxnet3;
210 #undef _
211 }
212 
213 /* set tup the API message handling tables */
214 clib_error_t *
216 {
217  vmxnet3_main_t *vmxm = &vmxnet3_main;
218  api_main_t *am = &api_main;
219  u8 *name;
220 
221  /* construct the API name */
222  name = format (0, "vmxnet3_%08x%c", api_version, 0);
223 
224  /* ask for a correctly-sized block of API message decode slots */
226  ((char *) name, VL_MSG_FIRST_AVAILABLE);
227 
228 #define _(N,n) \
229  vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base), \
230  #n, \
231  vl_api_##n##_t_handler, \
232  vl_noop_handler, \
233  vl_api_##n##_t_endian, \
234  vl_api_##n##_t_print, \
235  sizeof(vl_api_##n##_t), 1);
237 #undef _
238 
239  /* set up the (msg_name, crc, message-id) table */
240  setup_message_id_table (vmxm, am);
241 
242  vec_free (name);
243  return 0;
244 }
245 
246 /*
247  * fd.io coding-style-patch-verification: ON
248  *
249  * Local Variables:
250  * eval: (c-set-style "gnu")
251  * End:
252  */
void vmxnet3_delete_if(vlib_main_t *vm, vmxnet3_device_t *vd)
Definition: vmxnet3.c:636
Dump all vmxnet3 interfaces.
Definition: vmxnet3.api:115
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define REPLY_MACRO2(t, body)
#define NULL
Definition: clib.h:57
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
vlib_pci_addr_t pci_addr
Definition: vmxnet3.h:471
vmxnet3_main_t vmxnet3_main
Definition: vmxnet3.c:28
void vmxnet3_create_if(vlib_main_t *vm, vmxnet3_create_if_args_t *args)
Definition: vmxnet3.c:479
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
static void setup_message_id_table(vmxnet3_main_t *vmxm, api_main_t *am)
Definition: vmxnet3_api.c:205
static void vl_api_vmxnet3_create_t_handler(vl_api_vmxnet3_create_t *mp)
Definition: vmxnet3_api.c:60
memset(h->entries, 0, sizeof(h->entries[0])*entries)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:443
unsigned int u32
Definition: types.h:88
vlib_pci_addr_t addr
Definition: vmxnet3.h:506
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
Memory interface details structure.
Definition: vmxnet3.api:85
u16 msg_id_base
Definition: vmxnet3.h:498
unsigned short u16
Definition: types.h:57
vmxnet3_rx_comp_ring rx_comp_ring
Definition: vmxnet3.h:425
static void send_vmxnet3_details(vl_api_registration_t *reg, vmxnet3_device_t *vd, u16 rx_qid, vmxnet3_rxq_t *rxq, u16 tx_qid, vmxnet3_txq_t *txq, vnet_sw_interface_t *swif, u8 *interface_name, u32 context)
Definition: vmxnet3_api.c:113
vmxnet3_tx_comp_ring tx_comp_ring
Definition: vmxnet3.h:453
#define VMXNET3_RX_RING_SIZE
Definition: vmxnet3.h:95
#define REPLY_MACRO(t)
vnet_sw_interface_flags_t flags
Definition: interface.h:720
u8 name[64]
Definition: memclnt.api:151
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define foreach_vmxnet3_plugin_api_msg
Definition: vmxnet3_api.c:54
#define ARRAY_LEN(x)
Definition: clib.h:61
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
static void vl_api_vmxnet3_delete_t_handler(vl_api_vmxnet3_delete_t *mp)
Definition: vmxnet3_api.c:87
clib_error_t * vmxnet3_plugin_api_hookup(vlib_main_t *vm)
Definition: vmxnet3_api.c:215
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vnet_device_class_t vmxnet3_device_class
vmxnet3_rx_ring rx_ring[VMXNET3_RX_RING_SIZE]
Definition: vmxnet3.h:422
vmxnet3_device_t * devices
Definition: vmxnet3.h:495
static void vl_api_vmxnet3_dump_t_handler(vl_api_vmxnet3_dump_t *mp)
Message handler for vmxnet3_dump API.
Definition: vmxnet3_api.c:167
vmxnet3_tx_ring tx_ring
Definition: vmxnet3.h:452
api_main_t api_main
Definition: api_shared.c:35
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:865