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