Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 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_ether.h> |
| 21 | #include <linux/if_packet.h> |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 22 | #include <dirent.h> |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <fcntl.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 26 | |
| 27 | #include <vlib/vlib.h> |
| 28 | #include <vlib/unix/unix.h> |
| 29 | #include <vnet/ip/ip.h> |
| 30 | #include <vnet/ethernet/ethernet.h> |
| 31 | |
| 32 | #include <vnet/devices/af_packet/af_packet.h> |
| 33 | |
| 34 | #define AF_PACKET_DEBUG_SOCKET 0 |
| 35 | |
| 36 | #define AF_PACKET_TX_FRAMES_PER_BLOCK 1024 |
| 37 | #define AF_PACKET_TX_FRAME_SIZE (2048 * 5) |
| 38 | #define AF_PACKET_TX_BLOCK_NR 1 |
| 39 | #define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \ |
| 40 | AF_PACKET_TX_FRAMES_PER_BLOCK) |
| 41 | #define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \ |
| 42 | AF_PACKET_TX_FRAMES_PER_BLOCK) |
| 43 | |
| 44 | #define AF_PACKET_RX_FRAMES_PER_BLOCK 1024 |
| 45 | #define AF_PACKET_RX_FRAME_SIZE (2048 * 5) |
| 46 | #define AF_PACKET_RX_BLOCK_NR 1 |
| 47 | #define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \ |
| 48 | AF_PACKET_RX_FRAMES_PER_BLOCK) |
| 49 | #define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \ |
| 50 | AF_PACKET_RX_FRAMES_PER_BLOCK) |
| 51 | |
| 52 | #if AF_PACKET_DEBUG_SOCKET == 1 |
| 53 | #define DBG_SOCK(args...) clib_warning(args); |
| 54 | #else |
| 55 | #define DBG_SOCK(args...) |
| 56 | #endif |
| 57 | |
| 58 | /*defined in net/if.h but clashes with dpdk headers */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 59 | unsigned int if_nametoindex (const char *ifname); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 60 | |
| 61 | typedef struct tpacket_req tpacket_req_t; |
| 62 | |
| 63 | static u32 |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 64 | af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 65 | u32 flags) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 66 | { |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 67 | clib_error_t *error; |
| 68 | u8 *s; |
| 69 | af_packet_main_t *apm = &af_packet_main; |
| 70 | af_packet_if_t *apif = |
| 71 | pool_elt_at_index (apm->interfaces, hi->dev_instance); |
| 72 | |
| 73 | if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU)) |
| 74 | { |
| 75 | s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0); |
| 76 | |
| 77 | error = vlib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes); |
| 78 | vec_free (s); |
| 79 | |
| 80 | if (error) |
| 81 | { |
| 82 | clib_error_report (error); |
| 83 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 84 | } |
| 85 | } |
| 86 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 90 | static clib_error_t * |
| 91 | af_packet_fd_read_ready (unix_file_t * uf) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 92 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 93 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 94 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 95 | u32 idx = uf->private_data; |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 96 | af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 97 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 98 | apm->pending_input_bitmap = |
| 99 | clib_bitmap_set (apm->pending_input_bitmap, idx, 1); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 100 | |
| 101 | /* Schedule the rx node */ |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 102 | vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 108 | is_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 | |
| 126 | static int |
| 127 | create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 128 | tpacket_req_t * tx_req, int *fd, u8 ** ring) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 129 | { |
| 130 | int ret, err; |
| 131 | struct sockaddr_ll sll; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 132 | int ver = TPACKET_V2; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 133 | socklen_t req_sz = sizeof (struct tpacket_req); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 134 | u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr + |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 135 | tx_req->tp_block_size * tx_req->tp_block_nr; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 136 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 137 | if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 138 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 139 | DBG_SOCK ("Failed to create socket"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 140 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 141 | goto error; |
| 142 | } |
| 143 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 144 | if ((err = |
| 145 | setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 146 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 147 | DBG_SOCK ("Failed to set rx packet interface version"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 148 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | int opt = 1; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 153 | if ((err = |
| 154 | setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt))) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 155 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 156 | DBG_SOCK ("Failed to set packet tx ring error handling option"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 157 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 158 | goto error; |
| 159 | } |
| 160 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 161 | if ((err = |
| 162 | setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 163 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 164 | DBG_SOCK ("Failed to set packet rx ring options"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 165 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 166 | goto error; |
| 167 | } |
| 168 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 169 | if ((err = |
| 170 | setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 171 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 172 | DBG_SOCK ("Failed to set packet rx ring options"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 173 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 174 | goto error; |
| 175 | } |
| 176 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 177 | *ring = |
| 178 | mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd, |
| 179 | 0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 180 | if (*ring == MAP_FAILED) |
| 181 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 182 | DBG_SOCK ("mmap failure"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 183 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 184 | goto error; |
| 185 | } |
| 186 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 187 | memset (&sll, 0, sizeof (sll)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 188 | sll.sll_family = PF_PACKET; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 189 | sll.sll_protocol = htons (ETH_P_ALL); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 190 | sll.sll_ifindex = host_if_index; |
| 191 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 192 | if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 193 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 194 | DBG_SOCK ("Failed to bind rx packet socket (error %d)", err); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 195 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 196 | goto error; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | error: |
Dave Barach | 16ad6ae | 2016-07-28 17:55:30 -0400 | [diff] [blame] | 201 | if (*fd >= 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 202 | close (*fd); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 203 | *fd = -1; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | int |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 208 | af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set, |
| 209 | u32 * sw_if_index) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 210 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 211 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 212 | int ret, fd = -1; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 213 | struct tpacket_req *rx_req = 0; |
| 214 | struct tpacket_req *tx_req = 0; |
| 215 | u8 *ring = 0; |
| 216 | af_packet_if_t *apif = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 217 | u8 hw_addr[6]; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 218 | clib_error_t *error; |
| 219 | vnet_sw_interface_t *sw; |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 220 | vnet_hw_interface_t *hw; |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 221 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 222 | vnet_main_t *vnm = vnet_get_main (); |
| 223 | uword *p; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 224 | uword if_index; |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 225 | u8 *host_if_name_dup = vec_dup (host_if_name); |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 226 | int host_if_index = -1; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 227 | |
| 228 | p = mhash_get (&apm->if_index_by_host_if_name, host_if_name); |
| 229 | if (p) |
| 230 | { |
| 231 | return VNET_API_ERROR_SUBIF_ALREADY_EXISTS; |
| 232 | } |
| 233 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 234 | vec_validate (rx_req, 0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 235 | rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE; |
| 236 | rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE; |
| 237 | rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR; |
| 238 | rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR; |
| 239 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 240 | vec_validate (tx_req, 0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 241 | tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE; |
| 242 | tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE; |
| 243 | tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR; |
| 244 | tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR; |
| 245 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 246 | host_if_index = if_nametoindex ((const char *) host_if_name); |
| 247 | |
| 248 | if (!host_if_index) |
| 249 | { |
| 250 | DBG_SOCK ("Wrong host interface name"); |
| 251 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 252 | } |
| 253 | |
| 254 | ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 255 | |
| 256 | if (ret != 0) |
| 257 | goto error; |
| 258 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 259 | ret = is_bridge (host_if_name); |
| 260 | |
| 261 | if (ret == 0) /* is a bridge, ignore state */ |
| 262 | host_if_index = -1; |
| 263 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 264 | /* So far everything looks good, let's create interface */ |
Damjan Marion | 048ee2e | 2016-03-16 22:59:21 +0100 | [diff] [blame] | 265 | pool_get (apm->interfaces, apif); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 266 | if_index = apif - apm->interfaces; |
| 267 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 268 | apif->host_if_index = host_if_index; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 269 | apif->fd = fd; |
| 270 | apif->rx_ring = ring; |
| 271 | apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr; |
| 272 | apif->rx_req = rx_req; |
| 273 | apif->tx_req = tx_req; |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 274 | apif->host_if_name = host_if_name_dup; |
Dave Barach | 13f3c45 | 2016-03-29 11:56:41 -0400 | [diff] [blame] | 275 | apif->per_interface_next_index = ~0; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 276 | apif->next_tx_frame = 0; |
| 277 | apif->next_rx_frame = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 278 | |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 279 | if (tm->n_vlib_mains > 1) |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 280 | clib_spinlock_init (&apif->lockp); |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 281 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 282 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 283 | unix_file_t template = { 0 }; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 284 | template.read_function = af_packet_fd_read_ready; |
| 285 | template.file_descriptor = fd; |
| 286 | template.private_data = if_index; |
| 287 | template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED; |
| 288 | apif->unix_file_index = unix_file_add (&unix_main, &template); |
| 289 | } |
| 290 | |
| 291 | /*use configured or generate random MAC address */ |
| 292 | if (hw_addr_set) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 293 | clib_memcpy (hw_addr, hw_addr_set, 6); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 294 | else |
| 295 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 296 | f64 now = vlib_time_now (vm); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 297 | u32 rnd; |
| 298 | rnd = (u32) (now * 1e6); |
| 299 | rnd = random_u32 (&rnd); |
| 300 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 301 | clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 302 | hw_addr[0] = 2; |
| 303 | hw_addr[1] = 0xfe; |
| 304 | } |
| 305 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 306 | error = ethernet_register_interface (vnm, af_packet_device_class.index, |
| 307 | if_index, hw_addr, &apif->hw_if_index, |
| 308 | af_packet_eth_flag_change); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 309 | |
| 310 | if (error) |
| 311 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 312 | memset (apif, 0, sizeof (*apif)); |
| 313 | pool_put (apm->interfaces, apif); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 314 | clib_error_report (error); |
| 315 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index); |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 320 | hw = vnet_get_hw_interface (vnm, apif->hw_if_index); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 321 | apif->sw_if_index = sw->sw_if_index; |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 322 | vnet_hw_interface_set_input_node (vnm, apif->hw_if_index, |
| 323 | af_packet_input_node.index); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 324 | |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 325 | vnet_hw_interface_assign_rx_thread (vnm, apif->hw_if_index, 0, /* queue */ |
| 326 | ~0 /* any cpu */ ); |
| 327 | |
| 328 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 329 | vnet_hw_interface_set_flags (vnm, apif->hw_if_index, |
| 330 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 331 | |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 332 | vnet_hw_interface_set_rx_mode (vnm, apif->hw_if_index, 0, |
| 333 | VNET_HW_INTERFACE_RX_MODE_INTERRUPT); |
| 334 | |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 335 | mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index, |
| 336 | 0); |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 337 | if (sw_if_index) |
| 338 | *sw_if_index = apif->sw_if_index; |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 339 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | |
| 342 | error: |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 343 | vec_free (host_if_name_dup); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 344 | vec_free (rx_req); |
| 345 | vec_free (tx_req); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 349 | int |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 350 | af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name) |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 351 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 352 | vnet_main_t *vnm = vnet_get_main (); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 353 | af_packet_main_t *apm = &af_packet_main; |
| 354 | af_packet_if_t *apif; |
| 355 | uword *p; |
| 356 | uword if_index; |
| 357 | u32 ring_sz; |
| 358 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 359 | p = mhash_get (&apm->if_index_by_host_if_name, host_if_name); |
| 360 | if (p == NULL) |
| 361 | { |
| 362 | clib_warning ("Host interface %s does not exist", host_if_name); |
| 363 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 364 | } |
| 365 | apif = pool_elt_at_index (apm->interfaces, p[0]); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 366 | if_index = apif - apm->interfaces; |
| 367 | |
| 368 | /* bring down the interface */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 369 | vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0); |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 370 | vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 371 | |
| 372 | /* clean up */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 373 | if (apif->unix_file_index != ~0) |
| 374 | { |
| 375 | unix_file_del (&unix_main, unix_main.file_pool + apif->unix_file_index); |
| 376 | apif->unix_file_index = ~0; |
| 377 | } |
Eyal Bari | f298ecf | 2016-09-19 18:47:39 +0300 | [diff] [blame] | 378 | else |
| 379 | close (apif->fd); |
| 380 | |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 381 | ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr + |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 382 | apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr; |
| 383 | if (munmap (apif->rx_ring, ring_sz)) |
| 384 | clib_warning ("Host interface %s could not free rx/tx ring", |
| 385 | host_if_name); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 386 | apif->rx_ring = NULL; |
| 387 | apif->tx_ring = NULL; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 388 | apif->fd = -1; |
| 389 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 390 | vec_free (apif->rx_req); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 391 | apif->rx_req = NULL; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 392 | vec_free (apif->tx_req); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 393 | apif->tx_req = NULL; |
| 394 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 395 | vec_free (apif->host_if_name); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 396 | apif->host_if_name = NULL; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 397 | apif->host_if_index = -1; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 398 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 399 | mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 400 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 401 | ethernet_delete_interface (vnm, apif->hw_if_index); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 402 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 403 | pool_put (apm->interfaces, apif); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 408 | static clib_error_t * |
| 409 | af_packet_init (vlib_main_t * vm) |
| 410 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 411 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 553f6bd | 2016-09-07 11:54:22 +0200 | [diff] [blame] | 412 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 413 | |
| 414 | memset (apm, 0, sizeof (af_packet_main_t)); |
| 415 | |
| 416 | mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword)); |
| 417 | |
Damjan Marion | 553f6bd | 2016-09-07 11:54:22 +0200 | [diff] [blame] | 418 | vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1, |
| 419 | CLIB_CACHE_LINE_BYTES); |
| 420 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | VLIB_INIT_FUNCTION (af_packet_init); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * fd.io coding-style-patch-verification: ON |
| 428 | * |
| 429 | * Local Variables: |
| 430 | * eval: (c-set-style "gnu") |
| 431 | * End: |
| 432 | */ |