blob: 1fb4000f6e6f223e29986c7cdf3519a206cd912a [file] [log] [blame]
Damjan Marion83243a02016-02-29 13:09:30 +01001/*
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
Chris Luke4b46c842016-05-23 21:30:26 -040029#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")
Damjan Marion83243a02016-02-29 13:09:30 +010034
Damjan Marion00a9dca2016-08-17 17:05:46 +020035typedef enum
36{
Damjan Marion83243a02016-02-29 13:09:30 +010037#define _(f,s) AF_PACKET_TX_ERROR_##f,
38 foreach_af_packet_tx_func_error
39#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +020040 AF_PACKET_TX_N_ERROR,
Damjan Marion83243a02016-02-29 13:09:30 +010041} af_packet_tx_func_error_t;
42
Damjan Marion00a9dca2016-08-17 17:05:46 +020043static char *af_packet_tx_func_error_strings[] = {
Damjan Marion83243a02016-02-29 13:09:30 +010044#define _(n,s) s,
Damjan Marion00a9dca2016-08-17 17:05:46 +020045 foreach_af_packet_tx_func_error
Damjan Marion83243a02016-02-29 13:09:30 +010046#undef _
47};
48
49
Damjan Marion00a9dca2016-08-17 17:05:46 +020050static u8 *
51format_af_packet_device_name (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010052{
53 u32 i = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020054 af_packet_main_t *apm = &af_packet_main;
55 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
Damjan Marion83243a02016-02-29 13:09:30 +010056
57 s = format (s, "host-%s", apif->host_if_name);
58 return s;
59}
60
Damjan Marion00a9dca2016-08-17 17:05:46 +020061static u8 *
62format_af_packet_device (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010063{
64 s = format (s, "Linux PACKET socket interface");
65 return s;
66}
67
Damjan Marion00a9dca2016-08-17 17:05:46 +020068static u8 *
69format_af_packet_tx_trace (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010070{
71 s = format (s, "Unimplemented...");
72 return s;
73}
74
75static uword
76af_packet_interface_tx (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +020077 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marion83243a02016-02-29 13:09:30 +010078{
Damjan Marion00a9dca2016-08-17 17:05:46 +020079 af_packet_main_t *apm = &af_packet_main;
80 u32 *buffers = vlib_frame_args (frame);
Damjan Marion83243a02016-02-29 13:09:30 +010081 u32 n_left = frame->n_vectors;
82 u32 n_sent = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +020083 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);
Damjan Marion83243a02016-02-29 13:09:30 +010086 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;
Damjan Marion00a9dca2016-08-17 17:05:46 +020090 u8 *block_start = apif->tx_ring + block * block_size;
Damjan Marion83243a02016-02-29 13:09:30 +010091 u32 tx_frame = apif->next_tx_frame;
Damjan Marion00a9dca2016-08-17 17:05:46 +020092 struct tpacket2_hdr *tph;
Damjan Marion83243a02016-02-29 13:09:30 +010093 u32 frame_not_ready = 0;
94
Damjan Marion00a9dca2016-08-17 17:05:46 +020095 while (n_left > 0)
Damjan Marion83243a02016-02-29 13:09:30 +010096 {
97 u32 len;
98 u32 offset = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +020099 vlib_buffer_t *b0;
Damjan Marion83243a02016-02-29 13:09:30 +0100100 n_left--;
101 u32 bi = buffers[0];
102 buffers++;
103
104 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
105
Damjan Marion00a9dca2016-08-17 17:05:46 +0200106 if (PREDICT_FALSE
107 (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
Damjan Marion83243a02016-02-29 13:09:30 +0100108 {
109 frame_not_ready++;
110 goto next;
111 }
112
113 do
114 {
115 b0 = vlib_get_buffer (vm, bi);
116 len = b0->current_length;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200117 clib_memcpy ((u8 *) tph +
118 TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset,
119 vlib_buffer_get_current (b0), len);
Damjan Marion83243a02016-02-29 13:09:30 +0100120 offset += len;
121 }
122 while ((bi = b0->next_buffer));
123
124 tph->tp_len = tph->tp_snaplen = offset;
125 tph->tp_status = TP_STATUS_SEND_REQUEST;
126 n_sent++;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200127 next:
Chris Luke4b46c842016-05-23 21:30:26 -0400128 /* check if we've exhausted the ring */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200129 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
130 break;
Chris Luke4b46c842016-05-23 21:30:26 -0400131
Damjan Marion83243a02016-02-29 13:09:30 +0100132 tx_frame = (tx_frame + 1) % frame_num;
133 }
134
Damjan Marion00a9dca2016-08-17 17:05:46 +0200135 CLIB_MEMORY_BARRIER ();
Damjan Marion83243a02016-02-29 13:09:30 +0100136
Damjan Marion00a9dca2016-08-17 17:05:46 +0200137 if (PREDICT_TRUE (n_sent))
Damjan Marion83243a02016-02-29 13:09:30 +0100138 {
139 apif->next_tx_frame = tx_frame;
Chris Luke4b46c842016-05-23 21:30:26 -0400140
Damjan Marion00a9dca2016-08-17 17:05:46 +0200141 if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
142 MSG_DONTWAIT, NULL, 0) == -1))
143 {
144 /* Uh-oh, drop & move on, but count whether it was fatal or not.
145 * Note that we have no reliable way to properly determine the
146 * disposition of the packets we just enqueued for delivery.
147 */
148 vlib_error_count (vm, node->node_index,
149 unix_error_is_fatal (errno) ?
150 AF_PACKET_TX_ERROR_TXRING_FATAL :
151 AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
152 }
Damjan Marion83243a02016-02-29 13:09:30 +0100153 }
154
Damjan Marion00a9dca2016-08-17 17:05:46 +0200155 if (PREDICT_FALSE (frame_not_ready))
156 vlib_error_count (vm, node->node_index,
157 AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
Chris Luke4b46c842016-05-23 21:30:26 -0400158
Damjan Marion00a9dca2016-08-17 17:05:46 +0200159 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
Chris Luke4b46c842016-05-23 21:30:26 -0400160 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200161 n_left);
Damjan Marion83243a02016-02-29 13:09:30 +0100162
163 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
164 return frame->n_vectors;
165}
166
167static void
Damjan Marion00a9dca2016-08-17 17:05:46 +0200168af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
169 u32 node_index)
Damjan Marion83243a02016-02-29 13:09:30 +0100170{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200171 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100172 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200173 af_packet_if_t *apif =
174 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Damjan Marion83243a02016-02-29 13:09:30 +0100175
176 /* Shut off redirection */
177 if (node_index == ~0)
178 {
179 apif->per_interface_next_index = node_index;
180 return;
181 }
182
183 apif->per_interface_next_index =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200184 vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
185 node_index);
Damjan Marion83243a02016-02-29 13:09:30 +0100186}
187
Damjan Marion00a9dca2016-08-17 17:05:46 +0200188static void
189af_packet_clear_hw_interface_counters (u32 instance)
Damjan Marion83243a02016-02-29 13:09:30 +0100190{
191 /* Nothing for now */
192}
193
194static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +0200195af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
196 u32 flags)
Damjan Marion83243a02016-02-29 13:09:30 +0100197{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200198 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100199 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200200 af_packet_if_t *apif =
201 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700202 u32 hw_flags;
Damjan Marion83243a02016-02-29 13:09:30 +0100203
204 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
205
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700206 if (apif->is_admin_up)
207 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
208 else
209 hw_flags = 0;
210
Damjan Marion00a9dca2016-08-17 17:05:46 +0200211 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700212
Damjan Marion83243a02016-02-29 13:09:30 +0100213 return 0;
214}
215
216static clib_error_t *
217af_packet_subif_add_del_function (vnet_main_t * vnm,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200218 u32 hw_if_index,
219 struct vnet_sw_interface_t *st, int is_add)
Damjan Marion83243a02016-02-29 13:09:30 +0100220{
221 /* Nothing for now */
222 return 0;
223}
224
Damjan Marion00a9dca2016-08-17 17:05:46 +0200225/* *INDENT-OFF* */
Damjan Marion83243a02016-02-29 13:09:30 +0100226VNET_DEVICE_CLASS (af_packet_device_class) = {
227 .name = "af-packet",
228 .tx_function = af_packet_interface_tx,
229 .format_device_name = format_af_packet_device_name,
230 .format_device = format_af_packet_device,
231 .format_tx_trace = format_af_packet_tx_trace,
232 .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
233 .tx_function_error_strings = af_packet_tx_func_error_strings,
234 .rx_redirect_to_node = af_packet_set_interface_next_node,
235 .clear_counters = af_packet_clear_hw_interface_counters,
236 .admin_up_down_function = af_packet_interface_admin_up_down,
237 .subif_add_del_function = af_packet_subif_add_del_function,
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700238};
Damjan Marion1c80e832016-05-11 23:07:18 +0200239
240VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,
241 af_packet_interface_tx)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200242/* *INDENT-ON* */
243
244/*
245 * fd.io coding-style-patch-verification: ON
246 *
247 * Local Variables:
248 * eval: (c-set-style "gnu")
249 * End:
250 */