FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
device.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 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 <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <linux/virtio_net.h>
22 #include <linux/vhost.h>
23 
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/ip/ip4_packet.h>
28 #include <vnet/ip/ip6_packet.h>
30 
31 #define foreach_virtio_tx_func_error \
32 _(NO_FREE_SLOTS, "no free tx slots") \
33 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
34 _(PENDING_MSGS, "pending msgs in tx ring") \
35 _(NO_TX_QUEUES, "no tx queues")
36 
37 typedef enum
38 {
39 #define _(f,s) TAP_TX_ERROR_##f,
41 #undef _
44 
45 static char *virtio_tx_func_error_strings[] = {
46 #define _(n,s) s,
48 #undef _
49 };
50 
51 u8 *
52 format_virtio_device_name (u8 * s, va_list * args)
53 {
54  u32 dev_instance = va_arg (*args, u32);
56  virtio_if_t *vif = pool_elt_at_index (mm->interfaces, dev_instance);
57 
58  if (vif->type == VIRTIO_IF_TYPE_TAP)
59  {
60  s = format (s, "tap%u", vif->id);
61  }
62  else
63  s = format (s, "virtio%lu", vif->dev_instance);
64 
65  return s;
66 }
67 
68 static u8 *
69 format_virtio_device (u8 * s, va_list * args)
70 {
71  u32 dev_instance = va_arg (*args, u32);
72  int verbose = va_arg (*args, int);
73  u32 indent = format_get_indent (s);
74 
75  s = format (s, "VIRTIO interface");
76  if (verbose)
77  {
78  s = format (s, "\n%U instance %u", format_white_space, indent + 2,
79  dev_instance);
80  }
81  return s;
82 }
83 
84 static u8 *
85 format_virtio_tx_trace (u8 * s, va_list * args)
86 {
87  s = format (s, "Unimplemented...");
88  return s;
89 }
90 
91 inline void
93 {
94  u16 used = vring->desc_in_use;
95  u16 sz = vring->size;
96  u16 mask = sz - 1;
97  u16 last = vring->last_used_idx;
98  u16 n_left = vring->used->idx - last;
99 
100  if (n_left == 0)
101  return;
102 
103  while (n_left)
104  {
105  struct vring_used_elem *e = &vring->used->ring[last & mask];
106  u16 slot = e->id;
107  struct vring_desc *d = &vring->desc[slot];
108 
109  if (PREDICT_FALSE (d->flags & VRING_DESC_F_INDIRECT))
110  {
111  d = uword_to_pointer (d->addr, struct vring_desc *);
112  vec_free (d);
113  }
114 
115  vlib_buffer_free (vm, &vring->buffers[slot], 1);
116  used--;
117  last++;
118  n_left--;
119  }
120  vring->desc_in_use = used;
121  vring->last_used_idx = last;
122 }
123 
126  u16 avail, u16 next, u16 mask)
127 {
128  u16 n_added = 0;
129  const int hdr_sz = sizeof (struct virtio_net_hdr_v1);
130  struct vring_desc *d;
131  d = &vring->desc[next];
132  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
133 
134  if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
135  {
136  d->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
137  d->len = b->current_length + hdr_sz;
138  d->flags = 0;
139  }
140  else
141  {
142  struct vring_desc *id, *descs = 0;
143 
144  /* first buffer in chain */
145  vec_add2_aligned (descs, id, 1, CLIB_CACHE_LINE_BYTES);
146  id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
147  id->len = b->current_length + hdr_sz;
148 
149  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
150  {
151  id->flags = VRING_DESC_F_NEXT;
152  id->next = vec_len (descs);
153  vec_add2_aligned (descs, id, 1, CLIB_CACHE_LINE_BYTES);
154  b = vlib_get_buffer (vm, b->next_buffer);
155  id->addr = pointer_to_uword (vlib_buffer_get_current (b));
156  id->len = b->current_length;
157  }
158 
159  d->addr = pointer_to_uword (descs);
160  d->len = vec_len (descs) * sizeof (struct vring_desc);
161  d->flags = VRING_DESC_F_INDIRECT;
162  }
163  vring->buffers[next] = bi;
164  vring->avail->ring[avail & mask] = next;
165  n_added++;
166  return n_added;
167 }
168 
169 
172  vlib_frame_t * frame, virtio_if_t * vif)
173 {
174  u8 qid = 0;
175  u16 n_left = frame->n_vectors;
176  virtio_vring_t *vring = vec_elt_at_index (vif->vrings, (qid << 1) + 1);
177  u16 used, next, avail;
178  u16 sz = vring->size;
179  u16 mask = sz - 1;
180  u32 *buffers = vlib_frame_args (frame);
181 
182  /* free consumed buffers */
183  virtio_free_used_desc (vm, vring);
184 
185  used = vring->desc_in_use;
186  next = vring->desc_next;
187  avail = vring->avail->idx;
188 
189  while (n_left && used < sz)
190  {
191  u16 n_added;
192  n_added = add_buffer_to_slot (vm, vring, buffers[0], avail, next, mask);
193  avail += n_added;
194  next = (next + n_added) & mask;
195  used += n_added;
196  buffers++;
197  n_left--;
198  }
199 
200  if (n_left != frame->n_vectors)
201  {
203  vring->avail->idx = avail;
204  vring->desc_next = next;
205  vring->desc_in_use = used;
206  if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
207  {
208  u64 x = 1;
209  CLIB_UNUSED (int r) = write (vring->kick_fd, &x, sizeof (x));
210  }
211  }
212 
213 
214  if (n_left)
215  {
216  vlib_error_count (vm, node->node_index, TAP_TX_ERROR_NO_FREE_SLOTS,
217  n_left);
218  vlib_buffer_free (vm, buffers, n_left);
219  }
220 
221  return frame->n_vectors - n_left;
222 }
223 
224 static uword
226  vlib_node_runtime_t * node, vlib_frame_t * frame)
227 {
228  virtio_main_t *nm = &virtio_main;
229  vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
231 
232  return virtio_interface_tx_inline (vm, node, frame, vif);
233 }
234 
235 static void
237  u32 node_index)
238 {
239  virtio_main_t *apm = &virtio_main;
240  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
242 
243  /* Shut off redirection */
244  if (node_index == ~0)
245  {
246  vif->per_interface_next_index = node_index;
247  return;
248  }
249 
252  node_index);
253 }
254 
255 static void
257 {
258  /* Nothing for now */
259 }
260 
261 static clib_error_t *
264 {
265  virtio_main_t *mm = &virtio_main;
266  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
268  virtio_vring_t *vring = vec_elt_at_index (vif->vrings, qid);
269 
271  vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
272  else
273  vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
274 
275  return 0;
276 }
277 
278 static clib_error_t *
280 {
281  virtio_main_t *mm = &virtio_main;
282  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
284  static clib_error_t *error = 0;
285 
287  vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
288  else
289  vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
290 
291  return error;
292  return 0;
293 }
294 
295 static clib_error_t *
297  u32 hw_if_index,
298  struct vnet_sw_interface_t *st, int is_add)
299 {
300  /* Nothing for now */
301  return 0;
302 }
303 
304 /* *INDENT-OFF* */
306  .name = "virtio",
307  .tx_function = virtio_interface_tx,
308  .format_device_name = format_virtio_device_name,
309  .format_device = format_virtio_device,
310  .format_tx_trace = format_virtio_tx_trace,
311  .tx_function_n_errors = TAP_TX_N_ERROR,
312  .tx_function_error_strings = virtio_tx_func_error_strings,
313  .rx_redirect_to_node = virtio_set_interface_next_node,
314  .clear_counters = virtio_clear_hw_interface_counters,
315  .admin_up_down_function = virtio_interface_admin_up_down,
316  .subif_add_del_function = virtio_subif_add_del_function,
317  .rx_mode_change_function = virtio_interface_rx_mode_change,
318 };
319 
322 /* *INDENT-ON* */
323 
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "gnu")
329  * End:
330  */
u32 per_interface_next_index
Definition: virtio.h:98
struct vring_used * used
Definition: virtio.h:77
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
Definition: node.c:284
virtio_if_t * interfaces
Definition: virtio.h:122
#define CLIB_UNUSED(x)
Definition: clib.h:79
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:356
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:464
#define foreach_virtio_tx_func_error
Definition: device.c:31
#define PREDICT_TRUE(x)
Definition: clib.h:106
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:112
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
u32 dev_instance
Definition: virtio.h:95
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:569
int kick_fd
Definition: virtio.h:81
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
vnet_device_class_t virtio_device_class
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
virtio_vring_t * vrings
Definition: virtio.h:101
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1108
static char * virtio_tx_func_error_strings[]
Definition: device.c:45
virtio_tx_func_error_t
Definition: device.c:37
VNET_DEVICE_CLASS(af_packet_device_class)
vnet_hw_interface_rx_mode
Definition: interface.h:51
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
struct vring_avail * avail
Definition: virtio.h:78
#define static_always_inline
Definition: clib.h:93
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
static_always_inline uword virtio_interface_tx_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, virtio_if_t *vif)
Definition: device.c:171
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static void virtio_clear_hw_interface_counters(u32 instance)
Definition: device.c:256
unsigned long u64
Definition: types.h:89
static uword pointer_to_uword(const void *p)
Definition: types.h:131
u32 id
Definition: virtio.h:94
static clib_error_t * virtio_interface_rx_mode_change(vnet_main_t *vnm, u32 hw_if_index, u32 qid, vnet_hw_interface_rx_mode mode)
Definition: device.c:262
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:72
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:195
#define PREDICT_FALSE(x)
Definition: clib.h:105
u32 node_index
Node index.
Definition: node.h:437
static u8 * format_virtio_device(u8 *s, va_list *args)
Definition: device.c:69
#define uword_to_pointer(u, type)
Definition: types.h:136
u16 desc_next
Definition: virtio.h:80
static void virtio_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:236
u16 last_used_idx
Definition: virtio.h:88
u16 n_vectors
Definition: node.h:344
static u8 * format_virtio_tx_trace(u8 *s, va_list *args)
Definition: device.c:85
vlib_main_t * vm
Definition: buffer.c:283
#define VIRTIO_RING_FLAG_MASK_INT
Definition: virtio.h:84
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
static clib_error_t * virtio_subif_add_del_function(vnet_main_t *vnm, u32 hw_if_index, struct vnet_sw_interface_t *st, int is_add)
Definition: device.c:296
static clib_error_t * virtio_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:279
static_always_inline u16 add_buffer_to_slot(vlib_main_t *vm, virtio_vring_t *vring, u32 bi, u16 avail, u16 next, u16 mask)
Definition: device.c:125
u32 flags
Definition: virtio.h:93
virtio_if_type_t type
Definition: virtio.h:105
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:576
void virtio_free_used_desc(vlib_main_t *vm, virtio_vring_t *vring)
Definition: device.c:92
unsigned int u32
Definition: types.h:88
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:284
virtio_main_t virtio_main
Definition: virtio.c:35
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
u32 * buffers
Definition: virtio.h:87
struct vring_desc * desc
Definition: virtio.h:76
u8 * format_virtio_device_name(u8 *s, va_list *args)
Definition: device.c:52
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:75
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
#define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
Definition: interface.h:232
u16 desc_in_use
Definition: virtio.h:79
static uword virtio_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: device.c:225