1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2014 John W. Linville <linville@tuxdriver.com> 5 * 6 * Originally based upon librte_pmd_pcap code: 7 * 8 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved. 9 * Copyright(c) 2014 6WIND S.A. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * * Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * * Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in 20 * the documentation and/or other materials provided with the 21 * distribution. 22 * * Neither the name of Intel Corporation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <rte_mbuf.h> 40 #include <rte_ethdev.h> 41 #include <rte_malloc.h> 42 #include <rte_kvargs.h> 43 #include <rte_dev.h> 44 45 #include <linux/if_ether.h> 46 #include <linux/if_packet.h> 47 #include <arpa/inet.h> 48 #include <net/if.h> 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 #include <sys/ioctl.h> 52 #include <sys/mman.h> 53 #include <unistd.h> 54 #include <poll.h> 55 56 #define ETH_AF_PACKET_IFACE_ARG "iface" 57 #define ETH_AF_PACKET_NUM_Q_ARG "qpairs" 58 #define ETH_AF_PACKET_BLOCKSIZE_ARG "blocksz" 59 #define ETH_AF_PACKET_FRAMESIZE_ARG "framesz" 60 #define ETH_AF_PACKET_FRAMECOUNT_ARG "framecnt" 61 62 #define DFLT_BLOCK_SIZE (1 << 12) 63 #define DFLT_FRAME_SIZE (1 << 11) 64 #define DFLT_FRAME_COUNT (1 << 9) 65 66 #define RTE_PMD_AF_PACKET_MAX_RINGS 16 67 68 struct pkt_rx_queue { 69 int sockfd; 70 71 struct iovec *rd; 72 uint8_t *map; 73 unsigned int framecount; 74 unsigned int framenum; 75 76 struct rte_mempool *mb_pool; 77 uint8_t in_port; 78 79 volatile unsigned long rx_pkts; 80 volatile unsigned long err_pkts; 81 }; 82 83 struct pkt_tx_queue { 84 int sockfd; 85 86 struct iovec *rd; 87 uint8_t *map; 88 unsigned int framecount; 89 unsigned int framenum; 90 91 volatile unsigned long tx_pkts; 92 volatile unsigned long err_pkts; 93 }; 94 95 struct pmd_internals { 96 unsigned nb_queues; 97 98 int if_index; 99 struct ether_addr eth_addr; 100 101 struct tpacket_req req; 102 103 struct pkt_rx_queue rx_queue[RTE_PMD_AF_PACKET_MAX_RINGS]; 104 struct pkt_tx_queue tx_queue[RTE_PMD_AF_PACKET_MAX_RINGS]; 105 }; 106 107 static const char *valid_arguments[] = { 108 ETH_AF_PACKET_IFACE_ARG, 109 ETH_AF_PACKET_NUM_Q_ARG, 110 ETH_AF_PACKET_BLOCKSIZE_ARG, 111 ETH_AF_PACKET_FRAMESIZE_ARG, 112 ETH_AF_PACKET_FRAMECOUNT_ARG, 113 NULL 114 }; 115 116 static const char *drivername = "AF_PACKET PMD"; 117 118 static struct rte_eth_link pmd_link = { 119 .link_speed = 10000, 120 .link_duplex = ETH_LINK_FULL_DUPLEX, 121 .link_status = 0 122 }; 123 124 static uint16_t 125 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) 126 { 127 unsigned i; 128 struct tpacket2_hdr *ppd; 129 struct rte_mbuf *mbuf; 130 uint8_t *pbuf; 131 struct pkt_rx_queue *pkt_q = queue; 132 uint16_t num_rx = 0; 133 unsigned int framecount, framenum; 134 135 if (unlikely(nb_pkts == 0)) 136 return 0; 137 138 /* 139 * Reads the given number of packets from the AF_PACKET socket one by 140 * one and copies the packet data into a newly allocated mbuf. 141 */ 142 framecount = pkt_q->framecount; 143 framenum = pkt_q->framenum; 144 for (i = 0; i < nb_pkts; i++) { 145 /* point at the next incoming frame */ 146 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base; 147 if ((ppd->tp_status & TP_STATUS_USER) == 0) 148 break; 149 150 /* allocate the next mbuf */ 151 mbuf = rte_pktmbuf_alloc(pkt_q->mb_pool); 152 if (unlikely(mbuf == NULL)) 153 break; 154 155 /* packet will fit in the mbuf, go ahead and receive it */ 156 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf) = ppd->tp_snaplen; 157 pbuf = (uint8_t *) ppd + ppd->tp_mac; 158 memcpy(rte_pktmbuf_mtod(mbuf, void *), pbuf, rte_pktmbuf_data_len(mbuf)); 159 160 /* release incoming frame and advance ring buffer */ 161 ppd->tp_status = TP_STATUS_KERNEL; 162 if (++framenum >= framecount) 163 framenum = 0; 164 mbuf->port = pkt_q->in_port; 165 166 /* account for the receive frame */ 167 bufs[i] = mbuf; 168 num_rx++; 169 } 170 pkt_q->framenum = framenum; 171 pkt_q->rx_pkts += num_rx; 172 return num_rx; 173 } 174 175 /* 176 * Callback to handle sending packets through a real NIC. 177 */ 178 static uint16_t 179 eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) 180 { 181 struct tpacket2_hdr *ppd; 182 struct rte_mbuf *mbuf; 183 uint8_t *pbuf; 184 unsigned int framecount, framenum; 185 struct pollfd pfd; 186 struct pkt_tx_queue *pkt_q = queue; 187 uint16_t num_tx = 0; 188 int i; 189 190 if (unlikely(nb_pkts == 0)) 191 return 0; 192 193 memset(&pfd, 0, sizeof(pfd)); 194 pfd.fd = pkt_q->sockfd; 195 pfd.events = POLLOUT; 196 pfd.revents = 0; 197 198 framecount = pkt_q->framecount; 199 framenum = pkt_q->framenum; 200 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base; 201 for (i = 0; i < nb_pkts; i++) { 202 /* point at the next incoming frame */ 203 if ((ppd->tp_status != TP_STATUS_AVAILABLE) && 204 (poll(&pfd, 1, -1) < 0)) 205 continue; 206 207 /* copy the tx frame data */ 208 mbuf = bufs[num_tx]; 209 pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN - 210 sizeof(struct sockaddr_ll); 211 memcpy(pbuf, rte_pktmbuf_mtod(mbuf, void*), rte_pktmbuf_data_len(mbuf)); 212 ppd->tp_len = ppd->tp_snaplen = rte_pktmbuf_data_len(mbuf); 213 214 /* release incoming frame and advance ring buffer */ 215 ppd->tp_status = TP_STATUS_SEND_REQUEST; 216 if (++framenum >= framecount) 217 framenum = 0; 218 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base; 219 220 num_tx++; 221 rte_pktmbuf_free(mbuf); 222 } 223 224 /* kick-off transmits */ 225 if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1) 226 return 0; /* error sending -- no packets transmitted */ 227 228 pkt_q->framenum = framenum; 229 pkt_q->tx_pkts += num_tx; 230 pkt_q->err_pkts += nb_pkts - num_tx; 231 return num_tx; 232 } 233 234 static int 235 eth_dev_start(struct rte_eth_dev *dev) 236 { 237 dev->data->dev_link.link_status = 1; 238 return 0; 239 } 240 241 /* 242 * This function gets called when the current port gets stopped. 243 */ 244 static void 245 eth_dev_stop(struct rte_eth_dev *dev) 246 { 247 unsigned i; 248 int sockfd; 249 struct pmd_internals *internals = dev->data->dev_private; 250 251 for (i = 0; i < internals->nb_queues; i++) { 252 sockfd = internals->rx_queue[i].sockfd; 253 if (sockfd != -1) 254 close(sockfd); 255 sockfd = internals->tx_queue[i].sockfd; 256 if (sockfd != -1) 257 close(sockfd); 258 } 259 260 dev->data->dev_link.link_status = 0; 261 } 262 263 static int 264 eth_dev_configure(struct rte_eth_dev *dev __rte_unused) 265 { 266 return 0; 267 } 268 269 static void 270 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 271 { 272 struct pmd_internals *internals = dev->data->dev_private; 273 274 dev_info->driver_name = drivername; 275 dev_info->if_index = internals->if_index; 276 dev_info->max_mac_addrs = 1; 277 dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN; 278 dev_info->max_rx_queues = (uint16_t)internals->nb_queues; 279 dev_info->max_tx_queues = (uint16_t)internals->nb_queues; 280 dev_info->min_rx_bufsize = 0; 281 dev_info->pci_dev = NULL; 282 } 283 284 static void 285 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats) 286 { 287 unsigned i, imax; 288 unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0; 289 const struct pmd_internals *internal = dev->data->dev_private; 290 291 imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ? 292 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS); 293 for (i = 0; i < imax; i++) { 294 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; 295 rx_total += igb_stats->q_ipackets[i]; 296 } 297 298 imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ? 299 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS); 300 for (i = 0; i < imax; i++) { 301 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; 302 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts; 303 tx_total += igb_stats->q_opackets[i]; 304 tx_err_total += igb_stats->q_errors[i]; 305 } 306 307 igb_stats->ipackets = rx_total; 308 igb_stats->opackets = tx_total; 309 igb_stats->oerrors = tx_err_total; 310 } 311 312 static void 313 eth_stats_reset(struct rte_eth_dev *dev) 314 { 315 unsigned i; 316 struct pmd_internals *internal = dev->data->dev_private; 317 318 for (i = 0; i < internal->nb_queues; i++) 319 internal->rx_queue[i].rx_pkts = 0; 320 321 for (i = 0; i < internal->nb_queues; i++) { 322 internal->tx_queue[i].tx_pkts = 0; 323 internal->tx_queue[i].err_pkts = 0; 324 } 325 } 326 327 static void 328 eth_dev_close(struct rte_eth_dev *dev __rte_unused) 329 { 330 } 331 332 static void 333 eth_queue_release(void *q __rte_unused) 334 { 335 } 336 337 static int 338 eth_link_update(struct rte_eth_dev *dev __rte_unused, 339 int wait_to_complete __rte_unused) 340 { 341 return 0; 342 } 343 344 static int 345 eth_rx_queue_setup(struct rte_eth_dev *dev, 346 uint16_t rx_queue_id, 347 uint16_t nb_rx_desc __rte_unused, 348 unsigned int socket_id __rte_unused, 349 const struct rte_eth_rxconf *rx_conf __rte_unused, 350 struct rte_mempool *mb_pool) 351 { 352 struct pmd_internals *internals = dev->data->dev_private; 353 struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id]; 354 uint16_t buf_size; 355 356 pkt_q->mb_pool = mb_pool; 357 358 /* Now get the space available for data in the mbuf */ 359 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pkt_q->mb_pool) - 360 RTE_PKTMBUF_HEADROOM); 361 362 if (ETH_FRAME_LEN > buf_size) { 363 RTE_LOG(ERR, PMD, 364 "%s: %d bytes will not fit in mbuf (%d bytes)\n", 365 dev->data->name, ETH_FRAME_LEN, buf_size); 366 return -ENOMEM; 367 } 368 369 dev->data->rx_queues[rx_queue_id] = pkt_q; 370 pkt_q->in_port = dev->data->port_id; 371 372 return 0; 373 } 374 375 static int 376 eth_tx_queue_setup(struct rte_eth_dev *dev, 377 uint16_t tx_queue_id, 378 uint16_t nb_tx_desc __rte_unused, 379 unsigned int socket_id __rte_unused, 380 const struct rte_eth_txconf *tx_conf __rte_unused) 381 { 382 383 struct pmd_internals *internals = dev->data->dev_private; 384 385 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id]; 386 return 0; 387 } 388 389 static const struct eth_dev_ops ops = { 390 .dev_start = eth_dev_start, 391 .dev_stop = eth_dev_stop, 392 .dev_close = eth_dev_close, 393 .dev_configure = eth_dev_configure, 394 .dev_infos_get = eth_dev_info, 395 .rx_queue_setup = eth_rx_queue_setup, 396 .tx_queue_setup = eth_tx_queue_setup, 397 .rx_queue_release = eth_queue_release, 398 .tx_queue_release = eth_queue_release, 399 .link_update = eth_link_update, 400 .stats_get = eth_stats_get, 401 .stats_reset = eth_stats_reset, 402 }; 403 404 /* 405 * Opens an AF_PACKET socket 406 */ 407 static int 408 open_packet_iface(const char *key __rte_unused, 409 const char *value __rte_unused, 410 void *extra_args) 411 { 412 int *sockfd = extra_args; 413 414 /* Open an AF_PACKET socket... */ 415 *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 416 if (*sockfd == -1) { 417 RTE_LOG(ERR, PMD, "Could not open AF_PACKET socket\n"); 418 return -1; 419 } 420 421 return 0; 422 } 423 424 static int 425 rte_pmd_init_internals(const char *name, 426 const int sockfd, 427 const unsigned nb_queues, 428 unsigned int blocksize, 429 unsigned int blockcnt, 430 unsigned int framesize, 431 unsigned int framecnt, 432 const unsigned numa_node, 433 struct pmd_internals **internals, 434 struct rte_eth_dev **eth_dev, 435 struct rte_kvargs *kvlist) 436 { 437 struct rte_eth_dev_data *data = NULL; 438 struct rte_kvargs_pair *pair = NULL; 439 struct ifreq ifr; 440 size_t ifnamelen; 441 unsigned k_idx; 442 struct sockaddr_ll sockaddr; 443 struct tpacket_req *req; 444 struct pkt_rx_queue *rx_queue; 445 struct pkt_tx_queue *tx_queue; 446 int rc, tpver, discard; 447 int qsockfd = -1; 448 unsigned int i, q, rdsize; 449 int fanout_arg __rte_unused, bypass __rte_unused; 450 451 for (k_idx = 0; k_idx < kvlist->count; k_idx++) { 452 pair = &kvlist->pairs[k_idx]; 453 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL) 454 break; 455 } 456 if (pair == NULL) { 457 RTE_LOG(ERR, PMD, 458 "%s: no interface specified for AF_PACKET ethdev\n", 459 name); 460 goto error_early; 461 } 462 463 RTE_LOG(INFO, PMD, 464 "%s: creating AF_PACKET-backed ethdev on numa socket %u\n", 465 name, numa_node); 466 467 /* 468 * now do all data allocation - for eth_dev structure, dummy pci driver 469 * and internal (private) data 470 */ 471 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node); 472 if (data == NULL) 473 goto error_early; 474 475 *internals = rte_zmalloc_socket(name, sizeof(**internals), 476 0, numa_node); 477 if (*internals == NULL) 478 goto error_early; 479 480 for (q = 0; q < nb_queues; q++) { 481 (*internals)->rx_queue[q].map = MAP_FAILED; 482 (*internals)->tx_queue[q].map = MAP_FAILED; 483 } 484 485 req = &((*internals)->req); 486 487 req->tp_block_size = blocksize; 488 req->tp_block_nr = blockcnt; 489 req->tp_frame_size = framesize; 490 req->tp_frame_nr = framecnt; 491 492 ifnamelen = strlen(pair->value); 493 if (ifnamelen < sizeof(ifr.ifr_name)) { 494 memcpy(ifr.ifr_name, pair->value, ifnamelen); 495 ifr.ifr_name[ifnamelen] = '\0'; 496 } else { 497 RTE_LOG(ERR, PMD, 498 "%s: I/F name too long (%s)\n", 499 name, pair->value); 500 goto error_early; 501 } 502 if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) { 503 RTE_LOG(ERR, PMD, 504 "%s: ioctl failed (SIOCGIFINDEX)\n", 505 name); 506 goto error_early; 507 } 508 (*internals)->if_index = ifr.ifr_ifindex; 509 510 if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) { 511 RTE_LOG(ERR, PMD, 512 "%s: ioctl failed (SIOCGIFHWADDR)\n", 513 name); 514 goto error_early; 515 } 516 memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); 517 518 memset(&sockaddr, 0, sizeof(sockaddr)); 519 sockaddr.sll_family = AF_PACKET; 520 sockaddr.sll_protocol = htons(ETH_P_ALL); 521 sockaddr.sll_ifindex = (*internals)->if_index; 522 523 #if defined(PACKET_FANOUT) 524 fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff; 525 fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16; 526 #if defined(PACKET_FANOUT_FLAG_ROLLOVER) 527 fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16; 528 #endif 529 #endif 530 531 for (q = 0; q < nb_queues; q++) { 532 /* Open an AF_PACKET socket for this queue... */ 533 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 534 if (qsockfd == -1) { 535 RTE_LOG(ERR, PMD, 536 "%s: could not open AF_PACKET socket\n", 537 name); 538 return -1; 539 } 540 541 tpver = TPACKET_V2; 542 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION, 543 &tpver, sizeof(tpver)); 544 if (rc == -1) { 545 RTE_LOG(ERR, PMD, 546 "%s: could not set PACKET_VERSION on AF_PACKET " 547 "socket for %s\n", name, pair->value); 548 goto error; 549 } 550 551 discard = 1; 552 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS, 553 &discard, sizeof(discard)); 554 if (rc == -1) { 555 RTE_LOG(ERR, PMD, 556 "%s: could not set PACKET_LOSS on " 557 "AF_PACKET socket for %s\n", name, pair->value); 558 goto error; 559 } 560 561 #if defined(PACKET_QDISC_BYPASS) 562 bypass = 1; 563 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS, 564 &bypass, sizeof(bypass)); 565 if (rc == -1) { 566 RTE_LOG(ERR, PMD, 567 "%s: could not set PACKET_QDISC_BYPASS " 568 "on AF_PACKET socket for %s\n", name, 569 pair->value); 570 goto error; 571 } 572 #endif 573 574 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req)); 575 if (rc == -1) { 576 RTE_LOG(ERR, PMD, 577 "%s: could not set PACKET_RX_RING on AF_PACKET " 578 "socket for %s\n", name, pair->value); 579 goto error; 580 } 581 582 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req)); 583 if (rc == -1) { 584 RTE_LOG(ERR, PMD, 585 "%s: could not set PACKET_TX_RING on AF_PACKET " 586 "socket for %s\n", name, pair->value); 587 goto error; 588 } 589 590 rx_queue = &((*internals)->rx_queue[q]); 591 rx_queue->framecount = req->tp_frame_nr; 592 593 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr, 594 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, 595 qsockfd, 0); 596 if (rx_queue->map == MAP_FAILED) { 597 RTE_LOG(ERR, PMD, 598 "%s: call to mmap failed on AF_PACKET socket for %s\n", 599 name, pair->value); 600 goto error; 601 } 602 603 /* rdsize is same for both Tx and Rx */ 604 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd)); 605 606 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node); 607 if (rx_queue->rd == NULL) 608 goto error; 609 for (i = 0; i < req->tp_frame_nr; ++i) { 610 rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize); 611 rx_queue->rd[i].iov_len = req->tp_frame_size; 612 } 613 rx_queue->sockfd = qsockfd; 614 615 tx_queue = &((*internals)->tx_queue[q]); 616 tx_queue->framecount = req->tp_frame_nr; 617 618 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr; 619 620 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node); 621 if (tx_queue->rd == NULL) 622 goto error; 623 for (i = 0; i < req->tp_frame_nr; ++i) { 624 tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize); 625 tx_queue->rd[i].iov_len = req->tp_frame_size; 626 } 627 tx_queue->sockfd = qsockfd; 628 629 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr)); 630 if (rc == -1) { 631 RTE_LOG(ERR, PMD, 632 "%s: could not bind AF_PACKET socket to %s\n", 633 name, pair->value); 634 goto error; 635 } 636 637 #if defined(PACKET_FANOUT) 638 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT, 639 &fanout_arg, sizeof(fanout_arg)); 640 if (rc == -1) { 641 RTE_LOG(ERR, PMD, 642 "%s: could not set PACKET_FANOUT on AF_PACKET socket " 643 "for %s\n", name, pair->value); 644 goto error; 645 } 646 #endif 647 } 648 649 /* reserve an ethdev entry */ 650 *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL); 651 if (*eth_dev == NULL) 652 goto error; 653 654 /* 655 * now put it all together 656 * - store queue data in internals, 657 * - store numa_node in eth_dev 658 * - point eth_dev_data to internals 659 * - and point eth_dev structure to new eth_dev_data structure 660 */ 661 662 (*internals)->nb_queues = nb_queues; 663 664 data->dev_private = *internals; 665 data->port_id = (*eth_dev)->data->port_id; 666 data->nb_rx_queues = (uint16_t)nb_queues; 667 data->nb_tx_queues = (uint16_t)nb_queues; 668 data->dev_link = pmd_link; 669 data->mac_addrs = &(*internals)->eth_addr; 670 strncpy(data->name, 671 (*eth_dev)->data->name, strlen((*eth_dev)->data->name)); 672 673 (*eth_dev)->data = data; 674 (*eth_dev)->dev_ops = &ops; 675 (*eth_dev)->driver = NULL; 676 (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE; 677 (*eth_dev)->data->drv_name = drivername; 678 (*eth_dev)->data->kdrv = RTE_KDRV_NONE; 679 (*eth_dev)->data->numa_node = numa_node; 680 681 return 0; 682 683 error: 684 if (qsockfd != -1) 685 close(qsockfd); 686 for (q = 0; q < nb_queues; q++) { 687 munmap((*internals)->rx_queue[q].map, 688 2 * req->tp_block_size * req->tp_block_nr); 689 690 rte_free((*internals)->rx_queue[q].rd); 691 rte_free((*internals)->tx_queue[q].rd); 692 if (((*internals)->rx_queue[q].sockfd != 0) && 693 ((*internals)->rx_queue[q].sockfd != qsockfd)) 694 close((*internals)->rx_queue[q].sockfd); 695 } 696 rte_free(*internals); 697 error_early: 698 rte_free(data); 699 return -1; 700 } 701 702 static int 703 rte_eth_from_packet(const char *name, 704 int const *sockfd, 705 const unsigned numa_node, 706 struct rte_kvargs *kvlist) 707 { 708 struct pmd_internals *internals = NULL; 709 struct rte_eth_dev *eth_dev = NULL; 710 struct rte_kvargs_pair *pair = NULL; 711 unsigned k_idx; 712 unsigned int blockcount; 713 unsigned int blocksize = DFLT_BLOCK_SIZE; 714 unsigned int framesize = DFLT_FRAME_SIZE; 715 unsigned int framecount = DFLT_FRAME_COUNT; 716 unsigned int qpairs = 1; 717 718 /* do some parameter checking */ 719 if (*sockfd < 0) 720 return -1; 721 722 /* 723 * Walk arguments for configurable settings 724 */ 725 for (k_idx = 0; k_idx < kvlist->count; k_idx++) { 726 pair = &kvlist->pairs[k_idx]; 727 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) { 728 qpairs = atoi(pair->value); 729 if (qpairs < 1 || 730 qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) { 731 RTE_LOG(ERR, PMD, 732 "%s: invalid qpairs value\n", 733 name); 734 return -1; 735 } 736 continue; 737 } 738 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) { 739 blocksize = atoi(pair->value); 740 if (!blocksize) { 741 RTE_LOG(ERR, PMD, 742 "%s: invalid blocksize value\n", 743 name); 744 return -1; 745 } 746 continue; 747 } 748 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) { 749 framesize = atoi(pair->value); 750 if (!framesize) { 751 RTE_LOG(ERR, PMD, 752 "%s: invalid framesize value\n", 753 name); 754 return -1; 755 } 756 continue; 757 } 758 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) { 759 framecount = atoi(pair->value); 760 if (!framecount) { 761 RTE_LOG(ERR, PMD, 762 "%s: invalid framecount value\n", 763 name); 764 return -1; 765 } 766 continue; 767 } 768 } 769 770 if (framesize > blocksize) { 771 RTE_LOG(ERR, PMD, 772 "%s: AF_PACKET MMAP frame size exceeds block size!\n", 773 name); 774 return -1; 775 } 776 777 blockcount = framecount / (blocksize / framesize); 778 if (!blockcount) { 779 RTE_LOG(ERR, PMD, 780 "%s: invalid AF_PACKET MMAP parameters\n", name); 781 return -1; 782 } 783 784 RTE_LOG(INFO, PMD, "%s: AF_PACKET MMAP parameters:\n", name); 785 RTE_LOG(INFO, PMD, "%s:\tblock size %d\n", name, blocksize); 786 RTE_LOG(INFO, PMD, "%s:\tblock count %d\n", name, blockcount); 787 RTE_LOG(INFO, PMD, "%s:\tframe size %d\n", name, framesize); 788 RTE_LOG(INFO, PMD, "%s:\tframe count %d\n", name, framecount); 789 790 if (rte_pmd_init_internals(name, *sockfd, qpairs, 791 blocksize, blockcount, 792 framesize, framecount, 793 numa_node, &internals, ð_dev, 794 kvlist) < 0) 795 return -1; 796 797 eth_dev->rx_pkt_burst = eth_af_packet_rx; 798 eth_dev->tx_pkt_burst = eth_af_packet_tx; 799 800 return 0; 801 } 802 803 static int 804 rte_pmd_af_packet_devinit(const char *name, const char *params) 805 { 806 unsigned numa_node; 807 int ret = 0; 808 struct rte_kvargs *kvlist; 809 int sockfd = -1; 810 811 RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n", name); 812 813 numa_node = rte_socket_id(); 814 815 kvlist = rte_kvargs_parse(params, valid_arguments); 816 if (kvlist == NULL) { 817 ret = -1; 818 goto exit; 819 } 820 821 /* 822 * If iface argument is passed we open the NICs and use them for 823 * reading / writing 824 */ 825 if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) { 826 827 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG, 828 &open_packet_iface, &sockfd); 829 if (ret < 0) 830 goto exit; 831 } 832 833 ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist); 834 close(sockfd); /* no longer needed */ 835 836 exit: 837 rte_kvargs_free(kvlist); 838 return ret; 839 } 840 841 static int 842 rte_pmd_af_packet_devuninit(const char *name) 843 { 844 struct rte_eth_dev *eth_dev = NULL; 845 struct pmd_internals *internals; 846 unsigned q; 847 848 RTE_LOG(INFO, PMD, "Closing AF_PACKET ethdev on numa socket %u\n", 849 rte_socket_id()); 850 851 if (name == NULL) 852 return -1; 853 854 /* find the ethdev entry */ 855 eth_dev = rte_eth_dev_allocated(name); 856 if (eth_dev == NULL) 857 return -1; 858 859 internals = eth_dev->data->dev_private; 860 for (q = 0; q < internals->nb_queues; q++) { 861 rte_free(internals->rx_queue[q].rd); 862 rte_free(internals->tx_queue[q].rd); 863 } 864 865 rte_free(eth_dev->data->dev_private); 866 rte_free(eth_dev->data); 867 868 rte_eth_dev_release_port(eth_dev); 869 870 return 0; 871 } 872 873 static struct rte_driver pmd_af_packet_drv = { 874 .name = "eth_af_packet", 875 .type = PMD_VDEV, 876 .init = rte_pmd_af_packet_devinit, 877 .uninit = rte_pmd_af_packet_devuninit, 878 }; 879 880 PMD_REGISTER_DRIVER(pmd_af_packet_drv); 881