FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
device.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * af_packet.c - linux kernel packet interface
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <linux/if_packet.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <net/if.h>
24 #include <net/if_arp.h>
25 
26 #include <vlib/vlib.h>
27 #include <vlib/unix/unix.h>
28 #include <vnet/ip/ip.h>
29 #include <vnet/ethernet/ethernet.h>
30 
32 
33 #define foreach_af_packet_tx_func_error \
34 _(FRAME_NOT_READY, "tx frame not ready") \
35 _(TXRING_EAGAIN, "tx sendto temporary failure") \
36 _(TXRING_FATAL, "tx sendto fatal failure") \
37 _(TXRING_OVERRUN, "tx ring overrun")
38 
39 typedef enum
40 {
41 #define _(f,s) AF_PACKET_TX_ERROR_##f,
43 #undef _
46 
48 #define _(n,s) s,
50 #undef _
51 };
52 
53 
54 u8 *
55 format_af_packet_device_name (u8 * s, va_list * args)
56 {
57  u32 i = va_arg (*args, u32);
60 
61  s = format (s, "host-%s", apif->host_if_name);
62  return s;
63 }
64 
65 static u8 *
66 format_af_packet_device (u8 * s, va_list * args)
67 {
68  s = format (s, "Linux PACKET socket interface");
69  return s;
70 }
71 
72 static u8 *
73 format_af_packet_tx_trace (u8 * s, va_list * args)
74 {
75  s = format (s, "Unimplemented...");
76  return s;
77 }
78 
79 static uword
81  vlib_node_runtime_t * node, vlib_frame_t * frame)
82 {
84  u32 *buffers = vlib_frame_args (frame);
85  u32 n_left = frame->n_vectors;
86  u32 n_sent = 0;
87  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
88  af_packet_if_t *apif =
90  clib_spinlock_lock_if_init (&apif->lockp);
91  int block = 0;
92  u32 block_size = apif->tx_req->tp_block_size;
93  u32 frame_size = apif->tx_req->tp_frame_size;
94  u32 frame_num = apif->tx_req->tp_frame_nr;
95  u8 *block_start = apif->tx_ring + block * block_size;
96  u32 tx_frame = apif->next_tx_frame;
97  struct tpacket2_hdr *tph;
98  u32 frame_not_ready = 0;
99 
100  while (n_left > 0)
101  {
102  u32 len;
103  u32 offset = 0;
104  vlib_buffer_t *b0;
105  n_left--;
106  u32 bi = buffers[0];
107  buffers++;
108 
109  tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
110 
111  if (PREDICT_FALSE
112  (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
113  {
114  frame_not_ready++;
115  goto next;
116  }
117 
118  do
119  {
120  b0 = vlib_get_buffer (vm, bi);
121  len = b0->current_length;
122  clib_memcpy ((u8 *) tph +
123  TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset,
124  vlib_buffer_get_current (b0), len);
125  offset += len;
126  }
127  while ((bi =
128  (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
129 
130  tph->tp_len = tph->tp_snaplen = offset;
131  tph->tp_status = TP_STATUS_SEND_REQUEST;
132  n_sent++;
133  next:
134  tx_frame = (tx_frame + 1) % frame_num;
135 
136  /* check if we've exhausted the ring */
137  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
138  break;
139  }
140 
142 
143  if (PREDICT_TRUE (n_sent))
144  {
145  apif->next_tx_frame = tx_frame;
146 
147  if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
148  MSG_DONTWAIT, NULL, 0) == -1))
149  {
150  /* Uh-oh, drop & move on, but count whether it was fatal or not.
151  * Note that we have no reliable way to properly determine the
152  * disposition of the packets we just enqueued for delivery.
153  */
154  vlib_error_count (vm, node->node_index,
155  unix_error_is_fatal (errno) ?
156  AF_PACKET_TX_ERROR_TXRING_FATAL :
157  AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
158  }
159  }
160 
161  clib_spinlock_unlock_if_init (&apif->lockp);
162 
163  if (PREDICT_FALSE (frame_not_ready))
164  vlib_error_count (vm, node->node_index,
165  AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
166 
167  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
168  vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
169  n_left);
170 
171  vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
172  return frame->n_vectors;
173 }
174 
175 static void
177  u32 node_index)
178 {
180  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
181  af_packet_if_t *apif =
183 
184  /* Shut off redirection */
185  if (node_index == ~0)
186  {
187  apif->per_interface_next_index = node_index;
188  return;
189  }
190 
193  node_index);
194 }
195 
196 static void
198 {
199  /* Nothing for now */
200 }
201 
202 static clib_error_t *
204  u32 flags)
205 {
207  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
208  af_packet_if_t *apif =
210  u32 hw_flags;
211  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
212  struct ifreq ifr;
213 
214  if (0 > fd)
215  {
216  vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
217  apif->host_if_name);
218  return 0;
219  }
220 
221  /* if interface is a bridge ignore */
222  if (apif->host_if_index < 0)
223  goto error; /* no error */
224 
225  /* use host_if_index in case host name has changed */
226  ifr.ifr_ifindex = apif->host_if_index;
227  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
228  {
229  vlib_log_warn (apm->log_class,
230  "af_packet_%s ioctl could not retrieve eth name",
231  apif->host_if_name);
232  goto error;
233  }
234 
235  apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
236 
237  if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
238  {
239  vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
240  apif->is_admin_up ? "up" : "down", rv);
241  goto error;
242  }
243 
244  if (apif->is_admin_up)
245  {
247  ifr.ifr_flags |= IFF_UP;
248  }
249  else
250  {
251  hw_flags = 0;
252  ifr.ifr_flags &= ~IFF_UP;
253  }
254 
255  if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
256  {
257  vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
258  apif->is_admin_up ? "up" : "down", rv);
259  goto error;
260  }
261 
262  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
263 
264 error:
265  if (0 <= fd)
266  close (fd);
267 
268  return 0; /* no error */
269 }
270 
271 static clib_error_t *
273  u32 hw_if_index,
274  struct vnet_sw_interface_t *st, int is_add)
275 {
276  /* Nothing for now */
277  return 0;
278 }
279 
281  (struct vnet_hw_interface_t *hi, char *address)
282 {
284  af_packet_if_t *apif =
286  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
287  struct ifreq ifr;
288 
289  if (0 > fd)
290  {
291  vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
292  apif->host_if_name);
293  return 0;
294  }
295 
296  /* if interface is a bridge ignore */
297  if (apif->host_if_index < 0)
298  goto error; /* no error */
299 
300  /* use host_if_index in case host name has changed */
301  ifr.ifr_ifindex = apif->host_if_index;
302  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
303  {
305  (apm->log_class,
306  "af_packet_%s ioctl could not retrieve eth name, error: %d",
307  apif->host_if_name, rv);
308  goto error;
309  }
310 
311  clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
312  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
313 
314  if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
315  {
316  vlib_log_warn (apm->log_class,
317  "af_packet_%s ioctl could not set mac, error: %d",
318  apif->host_if_name, rv);
319  goto error;
320  }
321 
322 error:
323 
324  if (0 <= fd)
325  close (fd);
326 
327  return 0; /* no error */
328 }
329 
330 /* *INDENT-OFF* */
332  .name = "af-packet",
333  .tx_function = af_packet_interface_tx,
334  .format_device_name = format_af_packet_device_name,
335  .format_device = format_af_packet_device,
336  .format_tx_trace = format_af_packet_tx_trace,
337  .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
338  .tx_function_error_strings = af_packet_tx_func_error_strings,
339  .rx_redirect_to_node = af_packet_set_interface_next_node,
340  .clear_counters = af_packet_clear_hw_interface_counters,
341  .admin_up_down_function = af_packet_interface_admin_up_down,
342  .subif_add_del_function = af_packet_subif_add_del_function,
343  .mac_addr_change_function = af_packet_set_mac_address_function,
344 };
345 
348 /* *INDENT-ON* */
349 
350 /*
351  * fd.io coding-style-patch-verification: ON
352  *
353  * Local Variables:
354  * eval: (c-set-style "gnu")
355  * End:
356  */
#define vlib_log_warn(...)
Definition: log.h:51
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:541
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:534
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:500
#define PREDICT_TRUE(x)
Definition: clib.h:106
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#define NULL
Definition: clib.h:55
static u8 * format_af_packet_tx_trace(u8 *s, va_list *args)
Definition: device.c:73
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:98
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static void af_packet_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:176
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:458
static clib_error_t * af_packet_set_mac_address_function(struct vnet_hw_interface_t *hi, char *address)
Definition: device.c:281
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1110
unsigned char u8
Definition: types.h:56
u8 * host_if_name
Definition: af_packet.h:34
af_packet_if_t * interfaces
Definition: af_packet.h:55
unsigned int u32
Definition: types.h:88
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
static void af_packet_clear_hw_interface_counters(u32 instance)
Definition: device.c:197
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:202
#define PREDICT_FALSE(x)
Definition: clib.h:105
static char * af_packet_tx_func_error_strings[]
Definition: device.c:47
vlib_log_class_t log_class
log class
Definition: af_packet.h:67
u32 node_index
Node index.
Definition: node.h:473
u32 flags
Definition: vhost_user.h:110
u16 n_vectors
Definition: node.h:380
vlib_main_t * vm
Definition: buffer.c:294
static clib_error_t * af_packet_subif_add_del_function(vnet_main_t *vnm, u32 hw_if_index, struct vnet_sw_interface_t *st, int is_add)
Definition: device.c:272
#define clib_memcpy(a, b, c)
Definition: string.h:75
u32 per_interface_next_index
Definition: af_packet.h:48
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:370
#define foreach_af_packet_tx_func_error
Definition: device.c:33
int host_if_index
Definition: af_packet.h:35
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:661
af_packet_main_t af_packet_main
Definition: af_packet.c:37
static word unix_error_is_fatal(word error)
Definition: error.h:118
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:126
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:284
VNET_DEVICE_CLASS(bond_dev_class)
af_packet_tx_func_error_t
Definition: device.c:39
u8 * format_af_packet_device_name(u8 *s, va_list *args)
Definition: device.c:55
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vnet_device_class_t af_packet_device_class
template key/value backing page structure
Definition: bihash_doc.h:44
u64 uword
Definition: types.h:112
static clib_error_t * af_packet_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:203
struct clib_bihash_value offset
template key/value backing page structure
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:109
static uword af_packet_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: device.c:80
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:111
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:82
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
static u8 * format_af_packet_device(u8 *s, va_list *args)
Definition: device.c:66
#define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
Definition: interface.h:283