devices: af-packet v3 support
[vpp.git] / src / vnet / devices / af_packet / device.c
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
31 #include <vnet/devices/af_packet/af_packet.h>
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,
42   foreach_af_packet_tx_func_error
43 #undef _
44     AF_PACKET_TX_N_ERROR,
45 } af_packet_tx_func_error_t;
46
47 static char *af_packet_tx_func_error_strings[] = {
48 #define _(n,s) s,
49   foreach_af_packet_tx_func_error
50 #undef _
51 };
52
53
54 #ifndef CLIB_MARCH_VARIANT
55 u8 *
56 format_af_packet_device_name (u8 * s, va_list * args)
57 {
58   u32 i = va_arg (*args, u32);
59   af_packet_main_t *apm = &af_packet_main;
60   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
61
62   s = format (s, "host-%s", apif->host_if_name);
63   return s;
64 }
65 #endif /* CLIB_MARCH_VARIANT */
66
67 static u8 *
68 format_af_packet_device (u8 * s, va_list * args)
69 {
70   u32 dev_instance = va_arg (*args, u32);
71   u32 indent = format_get_indent (s);
72   int __clib_unused verbose = va_arg (*args, int);
73
74   af_packet_main_t *apm = &af_packet_main;
75   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance);
76   clib_spinlock_lock_if_init (&apif->lockp);
77   u32 tx_block_sz = apif->tx_req->tp_block_size;
78   u32 tx_frame_sz = apif->tx_req->tp_frame_size;
79   u32 tx_frame_nr = apif->tx_req->tp_frame_nr;
80   u32 tx_block_nr = apif->tx_req->tp_block_nr;
81   u32 rx_block_size = apif->rx_req->tp_block_size;
82   u32 rx_frame_size = apif->rx_req->tp_frame_size;
83   u32 rx_frame_nr = apif->rx_req->tp_frame_nr;
84   u32 rx_block_nr = apif->rx_req->tp_block_nr;
85   int block = 0;
86   u8 *tx_block_start = apif->tx_ring[block];
87   u32 tx_frame = apif->next_tx_frame;
88   tpacket3_hdr_t *tph;
89
90   s = format (s, "Linux PACKET socket interface\n");
91   s = format (s, "%UTX block size:%d nr:%d  TX frame size:%d nr:%d\n",
92               format_white_space, indent, tx_block_sz, tx_block_nr,
93               tx_frame_sz, tx_frame_nr);
94   s = format (s, "%URX block size:%d nr:%d  RX frame size:%d nr:%d\n",
95               format_white_space, indent, rx_block_size, rx_block_nr,
96               rx_frame_size, rx_frame_nr);
97   s = format (s, "%Unext frame:%d\n", format_white_space, indent,
98               apif->next_tx_frame);
99
100   int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0;
101   do
102     {
103       tph = (struct tpacket3_hdr *) (tx_block_start + tx_frame * tx_frame_sz);
104       tx_frame = (tx_frame + 1) % tx_frame_nr;
105       if (tph->tp_status == 0)
106         n_avail++;
107       else if (tph->tp_status & TP_STATUS_SEND_REQUEST)
108         n_send_req++;
109       else if (tph->tp_status & TP_STATUS_SENDING)
110         n_sending++;
111       else
112         n_wrong++;
113       n_tot++;
114     }
115   while (tx_frame != apif->next_tx_frame);
116   s = format (s, "%Uavailable:%d request:%d sending:%d wrong:%d total:%d\n",
117               format_white_space, indent, n_avail, n_send_req, n_sending,
118               n_wrong, n_tot);
119
120   clib_spinlock_unlock_if_init (&apif->lockp);
121   return s;
122 }
123
124 static u8 *
125 format_af_packet_tx_trace (u8 * s, va_list * args)
126 {
127   s = format (s, "Unimplemented...");
128   return s;
129 }
130
131 VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm,
132                                                   vlib_node_runtime_t * node,
133                                                   vlib_frame_t * frame)
134 {
135   af_packet_main_t *apm = &af_packet_main;
136   u32 *buffers = vlib_frame_vector_args (frame);
137   u32 n_left = frame->n_vectors;
138   u32 n_sent = 0;
139   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
140   af_packet_if_t *apif =
141     pool_elt_at_index (apm->interfaces, rd->dev_instance);
142   clib_spinlock_lock_if_init (&apif->lockp);
143   u32 block = 0;
144   u32 frame_size = apif->tx_req->tp_frame_size;
145   u32 frame_num = apif->tx_req->tp_frame_nr;
146   u8 *block_start = apif->tx_ring[block];
147   u32 tx_frame = apif->next_tx_frame;
148   tpacket3_hdr_t *tph;
149   u32 frame_not_ready = 0;
150
151   while (n_left)
152     {
153       u32 len;
154       u32 offset = 0;
155       vlib_buffer_t *b0;
156       n_left--;
157       u32 bi = buffers[0];
158       buffers++;
159
160       tph = (struct tpacket3_hdr *) (block_start + tx_frame * frame_size);
161       if (PREDICT_FALSE (tph->tp_status &
162                          (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
163         {
164           frame_not_ready++;
165           goto next;
166         }
167
168       do
169         {
170           b0 = vlib_get_buffer (vm, bi);
171           len = b0->current_length;
172           clib_memcpy_fast (
173             (u8 *) tph + TPACKET_ALIGN (sizeof (struct tpacket3_hdr)) + offset,
174             vlib_buffer_get_current (b0), len);
175           offset += len;
176         }
177       while ((bi =
178               (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
179
180       tph->tp_len = tph->tp_snaplen = offset;
181       tph->tp_status = TP_STATUS_SEND_REQUEST;
182       n_sent++;
183
184       tx_frame = (tx_frame + 1) % frame_num;
185
186     next:
187       /* check if we've exhausted the ring */
188       if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
189         break;
190     }
191
192   CLIB_MEMORY_BARRIER ();
193
194   if (PREDICT_TRUE (n_sent))
195     {
196       apif->next_tx_frame = tx_frame;
197
198       if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) ==
199                          -1))
200         {
201           /* Uh-oh, drop & move on, but count whether it was fatal or not.
202            * Note that we have no reliable way to properly determine the
203            * disposition of the packets we just enqueued for delivery.
204            */
205           vlib_error_count (vm, node->node_index,
206                             unix_error_is_fatal (errno) ?
207                               AF_PACKET_TX_ERROR_TXRING_FATAL :
208                               AF_PACKET_TX_ERROR_TXRING_EAGAIN,
209                             n_sent);
210         }
211     }
212
213   clib_spinlock_unlock_if_init (&apif->lockp);
214
215   if (PREDICT_FALSE (frame_not_ready))
216     vlib_error_count (vm, node->node_index,
217                       AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
218
219   if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
220     vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
221                       n_left);
222
223   vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
224   return frame->n_vectors;
225 }
226
227 static void
228 af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
229                                    u32 node_index)
230 {
231   af_packet_main_t *apm = &af_packet_main;
232   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
233   af_packet_if_t *apif =
234     pool_elt_at_index (apm->interfaces, hw->dev_instance);
235
236   /* Shut off redirection */
237   if (node_index == ~0)
238     {
239       apif->per_interface_next_index = node_index;
240       return;
241     }
242
243   apif->per_interface_next_index =
244     vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
245                         node_index);
246 }
247
248 static void
249 af_packet_clear_hw_interface_counters (u32 instance)
250 {
251   /* Nothing for now */
252 }
253
254 static clib_error_t *
255 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
256                                    u32 flags)
257 {
258   af_packet_main_t *apm = &af_packet_main;
259   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
260   af_packet_if_t *apif =
261     pool_elt_at_index (apm->interfaces, hw->dev_instance);
262   u32 hw_flags;
263   int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
264   struct ifreq ifr;
265
266   if (0 > fd)
267     {
268       vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
269                      apif->host_if_name);
270       return 0;
271     }
272
273   /* if interface is a bridge ignore */
274   if (apif->host_if_index < 0)
275     goto error;                 /* no error */
276
277   /* use host_if_index in case host name has changed */
278   ifr.ifr_ifindex = apif->host_if_index;
279   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
280     {
281       vlib_log_warn (apm->log_class,
282                      "af_packet_%s ioctl could not retrieve eth name",
283                      apif->host_if_name);
284       goto error;
285     }
286
287   apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
288
289   if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
290     {
291       vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
292                      apif->is_admin_up ? "up" : "down", rv);
293       goto error;
294     }
295
296   if (apif->is_admin_up)
297     {
298       hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
299       ifr.ifr_flags |= IFF_UP;
300     }
301   else
302     {
303       hw_flags = 0;
304       ifr.ifr_flags &= ~IFF_UP;
305     }
306
307   if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
308     {
309       vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
310                      apif->is_admin_up ? "up" : "down", rv);
311       goto error;
312     }
313
314   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
315
316 error:
317   if (0 <= fd)
318     close (fd);
319
320   return 0;                     /* no error */
321 }
322
323 static clib_error_t *
324 af_packet_subif_add_del_function (vnet_main_t * vnm,
325                                   u32 hw_if_index,
326                                   struct vnet_sw_interface_t *st, int is_add)
327 {
328   /* Nothing for now */
329   return 0;
330 }
331
332 static clib_error_t *af_packet_set_mac_address_function
333   (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
334 {
335   af_packet_main_t *apm = &af_packet_main;
336   af_packet_if_t *apif =
337     pool_elt_at_index (apm->interfaces, hi->dev_instance);
338   int rv, fd;
339   struct ifreq ifr;
340
341   if (apif->mode == AF_PACKET_IF_MODE_IP)
342     {
343       vlib_log_warn (apm->log_class, "af_packet_%s interface is in IP mode",
344                      apif->host_if_name);
345       return clib_error_return (0,
346                                 " MAC update failed, interface is in IP mode");
347     }
348
349   fd = socket (AF_UNIX, SOCK_DGRAM, 0);
350   if (0 > fd)
351     {
352       vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
353                      apif->host_if_name);
354       return 0;
355     }
356
357   /* if interface is a bridge ignore */
358   if (apif->host_if_index < 0)
359     goto error;                 /* no error */
360
361   /* use host_if_index in case host name has changed */
362   ifr.ifr_ifindex = apif->host_if_index;
363   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
364     {
365       vlib_log_warn
366         (apm->log_class,
367          "af_packet_%s ioctl could not retrieve eth name, error: %d",
368          apif->host_if_name, rv);
369       goto error;
370     }
371
372   clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
373   ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
374
375   if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
376     {
377       vlib_log_warn (apm->log_class,
378                      "af_packet_%s ioctl could not set mac, error: %d",
379                      apif->host_if_name, rv);
380       goto error;
381     }
382
383 error:
384
385   if (0 <= fd)
386     close (fd);
387
388   return 0;                     /* no error */
389 }
390
391 VNET_DEVICE_CLASS (af_packet_device_class) = {
392   .name = "af-packet",
393   .format_device_name = format_af_packet_device_name,
394   .format_device = format_af_packet_device,
395   .format_tx_trace = format_af_packet_tx_trace,
396   .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
397   .tx_function_error_strings = af_packet_tx_func_error_strings,
398   .rx_redirect_to_node = af_packet_set_interface_next_node,
399   .clear_counters = af_packet_clear_hw_interface_counters,
400   .admin_up_down_function = af_packet_interface_admin_up_down,
401   .subif_add_del_function = af_packet_subif_add_del_function,
402   .mac_addr_change_function = af_packet_set_mac_address_function,
403 };
404
405 /*
406  * fd.io coding-style-patch-verification: ON
407  *
408  * Local Variables:
409  * eval: (c-set-style "gnu")
410  * End:
411  */