af_packet: reflect admin device state on host
[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/ioctl.h>
22 #include <net/if.h>
23
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/ethernet/ethernet.h>
28
29 #include <vnet/devices/af_packet/af_packet.h>
30
31 #define foreach_af_packet_tx_func_error               \
32 _(FRAME_NOT_READY, "tx frame not ready")              \
33 _(TXRING_EAGAIN,   "tx sendto temporary failure")     \
34 _(TXRING_FATAL,    "tx sendto fatal failure")         \
35 _(TXRING_OVERRUN,  "tx ring overrun")
36
37 typedef enum
38 {
39 #define _(f,s) AF_PACKET_TX_ERROR_##f,
40   foreach_af_packet_tx_func_error
41 #undef _
42     AF_PACKET_TX_N_ERROR,
43 } af_packet_tx_func_error_t;
44
45 static char *af_packet_tx_func_error_strings[] = {
46 #define _(n,s) s,
47   foreach_af_packet_tx_func_error
48 #undef _
49 };
50
51
52 static u8 *
53 format_af_packet_device_name (u8 * s, va_list * args)
54 {
55   u32 i = va_arg (*args, u32);
56   af_packet_main_t *apm = &af_packet_main;
57   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
58
59   s = format (s, "host-%s", apif->host_if_name);
60   return s;
61 }
62
63 static u8 *
64 format_af_packet_device (u8 * s, va_list * args)
65 {
66   s = format (s, "Linux PACKET socket interface");
67   return s;
68 }
69
70 static u8 *
71 format_af_packet_tx_trace (u8 * s, va_list * args)
72 {
73   s = format (s, "Unimplemented...");
74   return s;
75 }
76
77 static uword
78 af_packet_interface_tx (vlib_main_t * vm,
79                         vlib_node_runtime_t * node, vlib_frame_t * frame)
80 {
81   af_packet_main_t *apm = &af_packet_main;
82   u32 *buffers = vlib_frame_args (frame);
83   u32 n_left = frame->n_vectors;
84   u32 n_sent = 0;
85   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
86   af_packet_if_t *apif =
87     pool_elt_at_index (apm->interfaces, rd->dev_instance);
88   int block = 0;
89   u32 block_size = apif->tx_req->tp_block_size;
90   u32 frame_size = apif->tx_req->tp_frame_size;
91   u32 frame_num = apif->tx_req->tp_frame_nr;
92   u8 *block_start = apif->tx_ring + block * block_size;
93   u32 tx_frame = apif->next_tx_frame;
94   struct tpacket2_hdr *tph;
95   u32 frame_not_ready = 0;
96
97   clib_spinlock_lock_if_init (&apif->lockp);
98
99   while (n_left > 0)
100     {
101       u32 len;
102       u32 offset = 0;
103       vlib_buffer_t *b0;
104       n_left--;
105       u32 bi = buffers[0];
106       buffers++;
107
108       tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
109
110       if (PREDICT_FALSE
111           (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
112         {
113           frame_not_ready++;
114           goto next;
115         }
116
117       do
118         {
119           b0 = vlib_get_buffer (vm, bi);
120           len = b0->current_length;
121           clib_memcpy ((u8 *) tph +
122                        TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset,
123                        vlib_buffer_get_current (b0), len);
124           offset += len;
125         }
126       while ((bi =
127               (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
128
129       tph->tp_len = tph->tp_snaplen = offset;
130       tph->tp_status = TP_STATUS_SEND_REQUEST;
131       n_sent++;
132     next:
133       /* check if we've exhausted the ring */
134       if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
135         break;
136
137       tx_frame = (tx_frame + 1) % frame_num;
138     }
139
140   CLIB_MEMORY_BARRIER ();
141
142   if (PREDICT_TRUE (n_sent))
143     {
144       apif->next_tx_frame = tx_frame;
145
146       if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
147                                  MSG_DONTWAIT, NULL, 0) == -1))
148         {
149           /* Uh-oh, drop & move on, but count whether it was fatal or not.
150            * Note that we have no reliable way to properly determine the
151            * disposition of the packets we just enqueued for delivery.
152            */
153           vlib_error_count (vm, node->node_index,
154                             unix_error_is_fatal (errno) ?
155                             AF_PACKET_TX_ERROR_TXRING_FATAL :
156                             AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
157         }
158     }
159
160   clib_spinlock_unlock_if_init (&apif->lockp);
161
162   if (PREDICT_FALSE (frame_not_ready))
163     vlib_error_count (vm, node->node_index,
164                       AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
165
166   if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
167     vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
168                       n_left);
169
170   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
171   return frame->n_vectors;
172 }
173
174 static void
175 af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
176                                    u32 node_index)
177 {
178   af_packet_main_t *apm = &af_packet_main;
179   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
180   af_packet_if_t *apif =
181     pool_elt_at_index (apm->interfaces, hw->dev_instance);
182
183   /* Shut off redirection */
184   if (node_index == ~0)
185     {
186       apif->per_interface_next_index = node_index;
187       return;
188     }
189
190   apif->per_interface_next_index =
191     vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
192                         node_index);
193 }
194
195 static void
196 af_packet_clear_hw_interface_counters (u32 instance)
197 {
198   /* Nothing for now */
199 }
200
201 static clib_error_t *
202 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
203                                    u32 flags)
204 {
205   af_packet_main_t *apm = &af_packet_main;
206   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
207   af_packet_if_t *apif =
208     pool_elt_at_index (apm->interfaces, hw->dev_instance);
209   u32 hw_flags;
210   int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
211   struct ifreq ifr;
212
213   /* if interface is a bridge ignore */
214   if (apif->host_if_index < 0)
215     return 0;                   /* no error */
216
217   /* use host_if_index in case host name has changed */
218   ifr.ifr_ifindex = apif->host_if_index;
219   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
220     {
221       clib_unix_warning ("af_packet_%s ioctl could not retrieve eth name",
222                          apif->host_if_name);
223     }
224
225   apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
226
227   if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
228     {
229       clib_unix_warning ("af_packet_%s error: %d",
230                          apif->is_admin_up ? "up" : "down", rv);
231     }
232
233   if (apif->is_admin_up)
234     {
235       hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
236       ifr.ifr_flags |= IFF_UP;
237     }
238   else
239     {
240       hw_flags = 0;
241       ifr.ifr_flags &= ~IFF_UP;
242     }
243
244   if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
245     {
246       clib_unix_warning ("af_packet_%s error: %d",
247                          apif->is_admin_up ? "up" : "down", rv);
248     }
249
250   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
251
252   close (fd);
253
254   return 0;                     /* no error */
255 }
256
257 static clib_error_t *
258 af_packet_subif_add_del_function (vnet_main_t * vnm,
259                                   u32 hw_if_index,
260                                   struct vnet_sw_interface_t *st, int is_add)
261 {
262   /* Nothing for now */
263   return 0;
264 }
265
266 /* *INDENT-OFF* */
267 VNET_DEVICE_CLASS (af_packet_device_class) = {
268   .name = "af-packet",
269   .tx_function = af_packet_interface_tx,
270   .format_device_name = format_af_packet_device_name,
271   .format_device = format_af_packet_device,
272   .format_tx_trace = format_af_packet_tx_trace,
273   .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
274   .tx_function_error_strings = af_packet_tx_func_error_strings,
275   .rx_redirect_to_node = af_packet_set_interface_next_node,
276   .clear_counters = af_packet_clear_hw_interface_counters,
277   .admin_up_down_function = af_packet_interface_admin_up_down,
278   .subif_add_del_function = af_packet_subif_add_del_function,
279 };
280
281 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,
282                                    af_packet_interface_tx)
283 /* *INDENT-ON* */
284
285 /*
286  * fd.io coding-style-patch-verification: ON
287  *
288  * Local Variables:
289  * eval: (c-set-style "gnu")
290  * End:
291  */