blob: 871b7bfa42720d99e5dbd2b80612fa6e63db01c9 [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_ether.h>
21#include <linux/if_packet.h>
Mohsin Kazmia50a14c2018-04-25 15:58:05 +020022#include <sys/ioctl.h>
23#include <net/if.h>
Ray Kinsellac855b732017-04-21 12:24:43 +010024#include <dirent.h>
Ray Kinsella7bfa1192017-05-15 11:52:43 +010025#include <sys/stat.h>
26#include <sys/types.h>
27#include <fcntl.h>
Damjan Marion83243a02016-02-29 13:09:30 +010028
Damjan Marion01914ce2017-09-14 19:04:50 +020029#include <vppinfra/linux/sysfs.h>
Damjan Marion83243a02016-02-29 13:09:30 +010030#include <vlib/vlib.h>
31#include <vlib/unix/unix.h>
32#include <vnet/ip/ip.h>
33#include <vnet/ethernet/ethernet.h>
34
35#include <vnet/devices/af_packet/af_packet.h>
36
Dave Wallace71612d62017-10-24 01:32:41 -040037af_packet_main_t af_packet_main;
38
Damjan Marion83243a02016-02-29 13:09:30 +010039#define AF_PACKET_TX_FRAMES_PER_BLOCK 1024
40#define AF_PACKET_TX_FRAME_SIZE (2048 * 5)
41#define AF_PACKET_TX_BLOCK_NR 1
42#define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \
43 AF_PACKET_TX_FRAMES_PER_BLOCK)
44#define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \
45 AF_PACKET_TX_FRAMES_PER_BLOCK)
46
47#define AF_PACKET_RX_FRAMES_PER_BLOCK 1024
48#define AF_PACKET_RX_FRAME_SIZE (2048 * 5)
49#define AF_PACKET_RX_BLOCK_NR 1
50#define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \
51 AF_PACKET_RX_FRAMES_PER_BLOCK)
52#define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \
53 AF_PACKET_RX_FRAMES_PER_BLOCK)
54
Damjan Marion83243a02016-02-29 13:09:30 +010055/*defined in net/if.h but clashes with dpdk headers */
Damjan Marion00a9dca2016-08-17 17:05:46 +020056unsigned int if_nametoindex (const char *ifname);
Damjan Marion83243a02016-02-29 13:09:30 +010057
58typedef struct tpacket_req tpacket_req_t;
59
60static u32
Damjan Marion00a9dca2016-08-17 17:05:46 +020061af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
62 u32 flags)
Damjan Marion83243a02016-02-29 13:09:30 +010063{
Ray Kinsella7bfa1192017-05-15 11:52:43 +010064 clib_error_t *error;
65 u8 *s;
66 af_packet_main_t *apm = &af_packet_main;
67 af_packet_if_t *apif =
68 pool_elt_at_index (apm->interfaces, hi->dev_instance);
69
70 if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU))
71 {
72 s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
73
Damjan Marion01914ce2017-09-14 19:04:50 +020074 error = clib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
Ray Kinsella7bfa1192017-05-15 11:52:43 +010075 vec_free (s);
76
77 if (error)
78 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +020079 vlib_log_err (apm->log_class,
80 "sysfs write failed to change MTU: %U",
81 format_clib_error, error);
82 clib_error_free (error);
Ray Kinsella7bfa1192017-05-15 11:52:43 +010083 return VNET_API_ERROR_SYSCALL_ERROR_1;
84 }
85 }
86
Damjan Marion83243a02016-02-29 13:09:30 +010087 return 0;
88}
89
Damjan Marion00a9dca2016-08-17 17:05:46 +020090static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +020091af_packet_fd_read_ready (clib_file_t * uf)
Damjan Marion83243a02016-02-29 13:09:30 +010092{
Damjan Marion00a9dca2016-08-17 17:05:46 +020093 af_packet_main_t *apm = &af_packet_main;
Damjan Marioneb743fa2017-03-20 16:34:15 +010094 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion83243a02016-02-29 13:09:30 +010095 u32 idx = uf->private_data;
Damjan Marioneb743fa2017-03-20 16:34:15 +010096 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx);
Damjan Marion83243a02016-02-29 13:09:30 +010097
Damjan Marion00a9dca2016-08-17 17:05:46 +020098 apm->pending_input_bitmap =
99 clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
Damjan Marion83243a02016-02-29 13:09:30 +0100100
101 /* Schedule the rx node */
Damjan Marioneb743fa2017-03-20 16:34:15 +0100102 vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100103
104 return 0;
105}
106
107static int
Ray Kinsellac855b732017-04-21 12:24:43 +0100108is_bridge (const u8 * host_if_name)
109{
110 u8 *s;
111 DIR *dir = NULL;
112
113 s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
114 dir = opendir ((char *) s);
115 vec_free (s);
116
117 if (dir)
118 {
119 closedir (dir);
120 return 0;
121 }
122
123 return -1;
124}
125
126static int
127create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200128 tpacket_req_t * tx_req, int *fd, u8 ** ring)
Damjan Marion83243a02016-02-29 13:09:30 +0100129{
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200130 af_packet_main_t *apm = &af_packet_main;
jackiechen19857fa41602019-05-07 18:59:13 +0800131 int ret;
Damjan Marion83243a02016-02-29 13:09:30 +0100132 struct sockaddr_ll sll;
Damjan Marion83243a02016-02-29 13:09:30 +0100133 int ver = TPACKET_V2;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200134 socklen_t req_sz = sizeof (struct tpacket_req);
Damjan Marion83243a02016-02-29 13:09:30 +0100135 u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
Damjan Marion00a9dca2016-08-17 17:05:46 +0200136 tx_req->tp_block_size * tx_req->tp_block_nr;
Damjan Marion83243a02016-02-29 13:09:30 +0100137
Damjan Marion00a9dca2016-08-17 17:05:46 +0200138 if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100139 {
jackiechen19857fa41602019-05-07 18:59:13 +0800140 vlib_log_debug (apm->log_class,
141 "Failed to create AF_PACKET socket: %s (errno %d)",
142 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100143 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
144 goto error;
145 }
Damjan Marionc5170202017-10-11 14:15:47 +0200146
Chaoyu Jinafb19302018-03-13 07:37:41 -0700147 /* bind before rx ring is cfged so we don't receive packets from other interfaces */
Dave Barachb7b92992018-10-17 10:38:51 -0400148 clib_memset (&sll, 0, sizeof (sll));
Chaoyu Jinafb19302018-03-13 07:37:41 -0700149 sll.sll_family = PF_PACKET;
150 sll.sll_protocol = htons (ETH_P_ALL);
151 sll.sll_ifindex = host_if_index;
jackiechen19857fa41602019-05-07 18:59:13 +0800152 if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
Chaoyu Jinafb19302018-03-13 07:37:41 -0700153 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200154 vlib_log_debug (apm->log_class,
jackiechen19857fa41602019-05-07 18:59:13 +0800155 "Failed to bind rx packet socket: %s (errno %d)",
156 strerror (errno), errno);
Chaoyu Jinafb19302018-03-13 07:37:41 -0700157 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
158 goto error;
159 }
160
jackiechen19857fa41602019-05-07 18:59:13 +0800161 if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100162 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200163 vlib_log_debug (apm->log_class,
jackiechen19857fa41602019-05-07 18:59:13 +0800164 "Failed to set rx packet interface version: %s (errno %d)",
165 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100166 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
167 goto error;
168 }
169
Damjan Marionc5170202017-10-11 14:15:47 +0200170 int opt = 1;
jackiechen19857fa41602019-05-07 18:59:13 +0800171 if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100172 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200173 vlib_log_debug (apm->log_class,
jackiechen19857fa41602019-05-07 18:59:13 +0800174 "Failed to set packet tx ring error handling option: %s (errno %d)",
175 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100176 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
177 goto error;
178 }
179
jackiechen19857fa41602019-05-07 18:59:13 +0800180 if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100181 {
jackiechen19857fa41602019-05-07 18:59:13 +0800182 vlib_log_debug (apm->log_class,
183 "Failed to set packet rx ring options: %s (errno %d)",
184 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100185 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
186 goto error;
187 }
188
jackiechen19857fa41602019-05-07 18:59:13 +0800189 if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100190 {
jackiechen19857fa41602019-05-07 18:59:13 +0800191 vlib_log_debug (apm->log_class,
192 "Failed to set packet tx ring options: %s (errno %d)",
193 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100194 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
195 goto error;
196 }
197
Damjan Marion00a9dca2016-08-17 17:05:46 +0200198 *ring =
199 mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
200 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100201 if (*ring == MAP_FAILED)
202 {
jackiechen19857fa41602019-05-07 18:59:13 +0800203 vlib_log_debug (apm->log_class, "mmap failure: %s (errno %d)",
204 strerror (errno), errno);
Damjan Marion83243a02016-02-29 13:09:30 +0100205 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
206 goto error;
207 }
208
Damjan Marion83243a02016-02-29 13:09:30 +0100209 return 0;
210error:
Dave Barach16ad6ae2016-07-28 17:55:30 -0400211 if (*fd >= 0)
jackiechen19857fa41602019-05-07 18:59:13 +0800212 {
213 close (*fd);
214 *fd = -1;
215 }
Damjan Marion83243a02016-02-29 13:09:30 +0100216 return ret;
217}
218
219int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200220af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
221 u32 * sw_if_index)
Damjan Marion83243a02016-02-29 13:09:30 +0100222{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200223 af_packet_main_t *apm = &af_packet_main;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200224 int ret, fd = -1, fd2 = -1;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200225 struct tpacket_req *rx_req = 0;
226 struct tpacket_req *tx_req = 0;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200227 struct ifreq ifr;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200228 u8 *ring = 0;
229 af_packet_if_t *apif = 0;
Damjan Marion83243a02016-02-29 13:09:30 +0100230 u8 hw_addr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200231 clib_error_t *error;
232 vnet_sw_interface_t *sw;
Damjan Marion44036902017-04-28 12:29:15 +0200233 vnet_hw_interface_t *hw;
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100234 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion00a9dca2016-08-17 17:05:46 +0200235 vnet_main_t *vnm = vnet_get_main ();
236 uword *p;
Damjan Marion83243a02016-02-29 13:09:30 +0100237 uword if_index;
jackiechen19857fa41602019-05-07 18:59:13 +0800238 u8 *host_if_name_dup = 0;
Ray Kinsellac855b732017-04-21 12:24:43 +0100239 int host_if_index = -1;
Damjan Marion83243a02016-02-29 13:09:30 +0100240
241 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
242 if (p)
243 {
Mohsin Kazmi43fc6882018-03-22 23:45:23 +0100244 apif = vec_elt_at_index (apm->interfaces, p[0]);
245 *sw_if_index = apif->sw_if_index;
246 return VNET_API_ERROR_IF_ALREADY_EXISTS;
Damjan Marion83243a02016-02-29 13:09:30 +0100247 }
248
jackiechen19857fa41602019-05-07 18:59:13 +0800249 host_if_name_dup = vec_dup (host_if_name);
250
Damjan Marion00a9dca2016-08-17 17:05:46 +0200251 vec_validate (rx_req, 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100252 rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
253 rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
254 rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
255 rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR;
256
Damjan Marion00a9dca2016-08-17 17:05:46 +0200257 vec_validate (tx_req, 0);
Damjan Marion83243a02016-02-29 13:09:30 +0100258 tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE;
259 tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE;
260 tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
261 tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
262
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200263 /*
264 * make sure host side of interface is 'UP' before binding AF_PACKET
265 * socket on it.
266 */
267 if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
Ray Kinsellac855b732017-04-21 12:24:43 +0100268 {
jackiechen19857fa41602019-05-07 18:59:13 +0800269 vlib_log_debug (apm->log_class,
270 "Failed to create AF_UNIX socket: %s (errno %d)",
271 strerror (errno), errno);
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200272 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
273 goto error;
274 }
275
276 clib_memcpy (ifr.ifr_name, (const char *) host_if_name,
277 vec_len (host_if_name));
jackiechen19857fa41602019-05-07 18:59:13 +0800278 if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200279 {
jackiechen19857fa41602019-05-07 18:59:13 +0800280 vlib_log_debug (apm->log_class,
281 "Failed to retrieve the interface (%s) index: %s (errno %d)",
282 host_if_name, strerror (errno), errno);
283 ret = VNET_API_ERROR_INVALID_INTERFACE;
284 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100285 }
286
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200287 host_if_index = ifr.ifr_ifindex;
jackiechen19857fa41602019-05-07 18:59:13 +0800288 if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200289 {
jackiechen19857fa41602019-05-07 18:59:13 +0800290 vlib_log_debug (apm->log_class,
291 "Failed to get the active flag: %s (errno %d)",
292 strerror (errno), errno);
293 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200294 goto error;
295 }
296
297 if (!(ifr.ifr_flags & IFF_UP))
298 {
299 ifr.ifr_flags |= IFF_UP;
jackiechen19857fa41602019-05-07 18:59:13 +0800300 if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200301 {
jackiechen19857fa41602019-05-07 18:59:13 +0800302 vlib_log_debug (apm->log_class,
303 "Failed to set the active flag: %s (errno %d)",
304 strerror (errno), errno);
305 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200306 goto error;
307 }
308 }
309
310 if (fd2 > -1)
jackiechen19857fa41602019-05-07 18:59:13 +0800311 {
312 close (fd2);
313 fd2 = -1;
314 }
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200315
Ray Kinsellac855b732017-04-21 12:24:43 +0100316 ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
Damjan Marion83243a02016-02-29 13:09:30 +0100317
318 if (ret != 0)
319 goto error;
320
Ray Kinsellac855b732017-04-21 12:24:43 +0100321 ret = is_bridge (host_if_name);
322
323 if (ret == 0) /* is a bridge, ignore state */
324 host_if_index = -1;
325
Damjan Marion83243a02016-02-29 13:09:30 +0100326 /* So far everything looks good, let's create interface */
Damjan Marion048ee2e2016-03-16 22:59:21 +0100327 pool_get (apm->interfaces, apif);
Damjan Marion83243a02016-02-29 13:09:30 +0100328 if_index = apif - apm->interfaces;
329
Ray Kinsellac855b732017-04-21 12:24:43 +0100330 apif->host_if_index = host_if_index;
Damjan Marion83243a02016-02-29 13:09:30 +0100331 apif->fd = fd;
332 apif->rx_ring = ring;
333 apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
334 apif->rx_req = rx_req;
335 apif->tx_req = tx_req;
Ivan Kellybfe737a2016-10-07 18:02:43 +0200336 apif->host_if_name = host_if_name_dup;
Dave Barach13f3c452016-03-29 11:56:41 -0400337 apif->per_interface_next_index = ~0;
Peter Leidba76f22016-04-08 08:16:31 -0700338 apif->next_tx_frame = 0;
339 apif->next_rx_frame = 0;
Damjan Marion83243a02016-02-29 13:09:30 +0100340
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100341 if (tm->n_vlib_mains > 1)
Damjan Marion1927da22017-03-27 17:08:20 +0200342 clib_spinlock_init (&apif->lockp);
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100343
Damjan Marion83243a02016-02-29 13:09:30 +0100344 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200345 clib_file_t template = { 0 };
Damjan Marion83243a02016-02-29 13:09:30 +0100346 template.read_function = af_packet_fd_read_ready;
347 template.file_descriptor = fd;
348 template.private_data = if_index;
349 template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
Damjan Marionceab7882018-01-19 20:56:12 +0100350 template.description = format (0, "%U", format_af_packet_device_name,
351 if_index);
Damjan Marion56dd5432017-09-08 19:52:02 +0200352 apif->clib_file_index = clib_file_add (&file_main, &template);
Damjan Marion83243a02016-02-29 13:09:30 +0100353 }
354
355 /*use configured or generate random MAC address */
356 if (hw_addr_set)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200357 clib_memcpy (hw_addr, hw_addr_set, 6);
Damjan Marion83243a02016-02-29 13:09:30 +0100358 else
359 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200360 f64 now = vlib_time_now (vm);
Damjan Marion83243a02016-02-29 13:09:30 +0100361 u32 rnd;
362 rnd = (u32) (now * 1e6);
363 rnd = random_u32 (&rnd);
364
Damjan Marion00a9dca2016-08-17 17:05:46 +0200365 clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
Damjan Marion83243a02016-02-29 13:09:30 +0100366 hw_addr[0] = 2;
367 hw_addr[1] = 0xfe;
368 }
369
Damjan Marion00a9dca2016-08-17 17:05:46 +0200370 error = ethernet_register_interface (vnm, af_packet_device_class.index,
371 if_index, hw_addr, &apif->hw_if_index,
372 af_packet_eth_flag_change);
Damjan Marion83243a02016-02-29 13:09:30 +0100373
374 if (error)
375 {
Dave Barachb7b92992018-10-17 10:38:51 -0400376 clib_memset (apif, 0, sizeof (*apif));
Damjan Marion00a9dca2016-08-17 17:05:46 +0200377 pool_put (apm->interfaces, apif);
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200378 vlib_log_err (apm->log_class, "Unable to register interface: %U",
379 format_clib_error, error);
380 clib_error_free (error);
Damjan Marion83243a02016-02-29 13:09:30 +0100381 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
382 goto error;
383 }
384
385 sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +0200386 hw = vnet_get_hw_interface (vnm, apif->hw_if_index);
Damjan Marion83243a02016-02-29 13:09:30 +0100387 apif->sw_if_index = sw->sw_if_index;
Damjan Marion44036902017-04-28 12:29:15 +0200388 vnet_hw_interface_set_input_node (vnm, apif->hw_if_index,
389 af_packet_input_node.index);
Damjan Marion83243a02016-02-29 13:09:30 +0100390
Damjan Marion44036902017-04-28 12:29:15 +0200391 vnet_hw_interface_assign_rx_thread (vnm, apif->hw_if_index, 0, /* queue */
392 ~0 /* any cpu */ );
393
394 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Damjan Marion83243a02016-02-29 13:09:30 +0100395 vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
396 VNET_HW_INTERFACE_FLAG_LINK_UP);
397
Damjan Marion44036902017-04-28 12:29:15 +0200398 vnet_hw_interface_set_rx_mode (vnm, apif->hw_if_index, 0,
399 VNET_HW_INTERFACE_RX_MODE_INTERRUPT);
400
Ivan Kellybfe737a2016-10-07 18:02:43 +0200401 mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
402 0);
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100403 if (sw_if_index)
404 *sw_if_index = apif->sw_if_index;
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100405
Damjan Marion83243a02016-02-29 13:09:30 +0100406 return 0;
407
408error:
Mohsin Kazmia50a14c2018-04-25 15:58:05 +0200409 if (fd2 > -1)
jackiechen19857fa41602019-05-07 18:59:13 +0800410 {
411 close (fd2);
412 fd2 = -1;
413 }
Ivan Kellybfe737a2016-10-07 18:02:43 +0200414 vec_free (host_if_name_dup);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200415 vec_free (rx_req);
416 vec_free (tx_req);
Damjan Marion83243a02016-02-29 13:09:30 +0100417 return ret;
418}
419
Peter Leidba76f22016-04-08 08:16:31 -0700420int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200421af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
Peter Leidba76f22016-04-08 08:16:31 -0700422{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200423 vnet_main_t *vnm = vnet_get_main ();
Peter Leidba76f22016-04-08 08:16:31 -0700424 af_packet_main_t *apm = &af_packet_main;
425 af_packet_if_t *apif;
426 uword *p;
427 uword if_index;
428 u32 ring_sz;
429
Damjan Marion00a9dca2016-08-17 17:05:46 +0200430 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
431 if (p == NULL)
432 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200433 vlib_log_warn (apm->log_class, "Host interface %s does not exist",
434 host_if_name);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200435 return VNET_API_ERROR_SYSCALL_ERROR_1;
436 }
437 apif = pool_elt_at_index (apm->interfaces, p[0]);
Peter Leidba76f22016-04-08 08:16:31 -0700438 if_index = apif - apm->interfaces;
439
440 /* bring down the interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200441 vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
Damjan Marion44036902017-04-28 12:29:15 +0200442 vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0);
Peter Leidba76f22016-04-08 08:16:31 -0700443
444 /* clean up */
Damjan Marion56dd5432017-09-08 19:52:02 +0200445 if (apif->clib_file_index != ~0)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200446 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200447 clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index);
448 apif->clib_file_index = ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200449 }
Eyal Barif298ecf2016-09-19 18:47:39 +0300450 else
451 close (apif->fd);
452
Peter Leidba76f22016-04-08 08:16:31 -0700453 ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
Damjan Marion00a9dca2016-08-17 17:05:46 +0200454 apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
455 if (munmap (apif->rx_ring, ring_sz))
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200456 vlib_log_warn (apm->log_class,
457 "Host interface %s could not free rx/tx ring",
458 host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700459 apif->rx_ring = NULL;
460 apif->tx_ring = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700461 apif->fd = -1;
462
Damjan Marion00a9dca2016-08-17 17:05:46 +0200463 vec_free (apif->rx_req);
Peter Leidba76f22016-04-08 08:16:31 -0700464 apif->rx_req = NULL;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200465 vec_free (apif->tx_req);
Peter Leidba76f22016-04-08 08:16:31 -0700466 apif->tx_req = NULL;
467
Damjan Marion00a9dca2016-08-17 17:05:46 +0200468 vec_free (apif->host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700469 apif->host_if_name = NULL;
Ray Kinsellac855b732017-04-21 12:24:43 +0100470 apif->host_if_index = -1;
Peter Leidba76f22016-04-08 08:16:31 -0700471
Damjan Marion00a9dca2016-08-17 17:05:46 +0200472 mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);
Peter Leidba76f22016-04-08 08:16:31 -0700473
Damjan Marion00a9dca2016-08-17 17:05:46 +0200474 ethernet_delete_interface (vnm, apif->hw_if_index);
Peter Leidba76f22016-04-08 08:16:31 -0700475
Damjan Marion00a9dca2016-08-17 17:05:46 +0200476 pool_put (apm->interfaces, apif);
Peter Leidba76f22016-04-08 08:16:31 -0700477
478 return 0;
479}
480
Jakub Grajciar92b02752017-10-20 13:37:28 +0200481int
482af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index, u8 set)
483{
484 vnet_main_t *vnm = vnet_get_main ();
485 vnet_hw_interface_t *hw;
486
487 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
488
Jakub Grajciar64ae7782017-11-14 13:45:04 +0100489 if (hw->dev_class_index != af_packet_device_class.index)
490 return VNET_API_ERROR_INVALID_INTERFACE;
491
Jakub Grajciar92b02752017-10-20 13:37:28 +0200492 if (set)
493 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
494 else
495 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
496
497 return 0;
498}
499
Mohsin Kazmi04e0bb22018-05-28 18:55:37 +0200500int
501af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
502{
503 af_packet_main_t *apm = &af_packet_main;
504 af_packet_if_t *apif;
505 af_packet_if_detail_t *r_af_packet_ifs = NULL;
506 af_packet_if_detail_t *af_packet_if = NULL;
507
508 vec_foreach (apif, apm->interfaces)
509 {
510 vec_add2 (r_af_packet_ifs, af_packet_if, 1);
511 af_packet_if->sw_if_index = apif->sw_if_index;
512 if (apif->host_if_name)
513 {
514 clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
515 MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
516 strlen ((const char *) apif->host_if_name)));
517 }
518 }
519
520 *out_af_packet_ifs = r_af_packet_ifs;
521
522 return 0;
523}
524
Damjan Marion83243a02016-02-29 13:09:30 +0100525static clib_error_t *
526af_packet_init (vlib_main_t * vm)
527{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200528 af_packet_main_t *apm = &af_packet_main;
Damjan Marion553f6bd2016-09-07 11:54:22 +0200529 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion83243a02016-02-29 13:09:30 +0100530
Dave Barachb7b92992018-10-17 10:38:51 -0400531 clib_memset (apm, 0, sizeof (af_packet_main_t));
Damjan Marion83243a02016-02-29 13:09:30 +0100532
533 mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
534
Damjan Marion553f6bd2016-09-07 11:54:22 +0200535 vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
536 CLIB_CACHE_LINE_BYTES);
537
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200538 apm->log_class = vlib_log_register_class ("af_packet", 0);
539 vlib_log_debug (apm->log_class, "initialized");
540
Damjan Marion83243a02016-02-29 13:09:30 +0100541 return 0;
542}
543
544VLIB_INIT_FUNCTION (af_packet_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200545
546/*
547 * fd.io coding-style-patch-verification: ON
548 *
549 * Local Variables:
550 * eval: (c-set-style "gnu")
551 * End:
552 */