New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / af_packet / rte_eth_af_packet.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014 John W. Linville <linville@tuxdriver.com>
3  * Originally based upon librte_pmd_pcap code:
4  * Copyright(c) 2010-2015 Intel Corporation.
5  * Copyright(c) 2014 6WIND S.A.
6  * All rights reserved.
7  */
8
9 #include <rte_mbuf.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_ethdev_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_kvargs.h>
14 #include <rte_bus_vdev.h>
15
16 #include <linux/if_ether.h>
17 #include <linux/if_packet.h>
18 #include <arpa/inet.h>
19 #include <net/if.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <sys/mman.h>
24 #include <unistd.h>
25 #include <poll.h>
26
27 #define ETH_AF_PACKET_IFACE_ARG         "iface"
28 #define ETH_AF_PACKET_NUM_Q_ARG         "qpairs"
29 #define ETH_AF_PACKET_BLOCKSIZE_ARG     "blocksz"
30 #define ETH_AF_PACKET_FRAMESIZE_ARG     "framesz"
31 #define ETH_AF_PACKET_FRAMECOUNT_ARG    "framecnt"
32 #define ETH_AF_PACKET_QDISC_BYPASS_ARG  "qdisc_bypass"
33
34 #define DFLT_BLOCK_SIZE         (1 << 12)
35 #define DFLT_FRAME_SIZE         (1 << 11)
36 #define DFLT_FRAME_COUNT        (1 << 9)
37
38 #define RTE_PMD_AF_PACKET_MAX_RINGS 16
39
40 struct pkt_rx_queue {
41         int sockfd;
42
43         struct iovec *rd;
44         uint8_t *map;
45         unsigned int framecount;
46         unsigned int framenum;
47
48         struct rte_mempool *mb_pool;
49         uint16_t in_port;
50
51         volatile unsigned long rx_pkts;
52         volatile unsigned long err_pkts;
53         volatile unsigned long rx_bytes;
54 };
55
56 struct pkt_tx_queue {
57         int sockfd;
58         unsigned int frame_data_size;
59
60         struct iovec *rd;
61         uint8_t *map;
62         unsigned int framecount;
63         unsigned int framenum;
64
65         volatile unsigned long tx_pkts;
66         volatile unsigned long err_pkts;
67         volatile unsigned long tx_bytes;
68 };
69
70 struct pmd_internals {
71         unsigned nb_queues;
72
73         int if_index;
74         char *if_name;
75         struct ether_addr eth_addr;
76
77         struct tpacket_req req;
78
79         struct pkt_rx_queue rx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
80         struct pkt_tx_queue tx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
81 };
82
83 static const char *valid_arguments[] = {
84         ETH_AF_PACKET_IFACE_ARG,
85         ETH_AF_PACKET_NUM_Q_ARG,
86         ETH_AF_PACKET_BLOCKSIZE_ARG,
87         ETH_AF_PACKET_FRAMESIZE_ARG,
88         ETH_AF_PACKET_FRAMECOUNT_ARG,
89         ETH_AF_PACKET_QDISC_BYPASS_ARG,
90         NULL
91 };
92
93 static struct rte_eth_link pmd_link = {
94         .link_speed = ETH_SPEED_NUM_10G,
95         .link_duplex = ETH_LINK_FULL_DUPLEX,
96         .link_status = ETH_LINK_DOWN,
97         .link_autoneg = ETH_LINK_FIXED,
98 };
99
100 static int af_packet_logtype;
101
102 #define PMD_LOG(level, fmt, args...) \
103         rte_log(RTE_LOG_ ## level, af_packet_logtype, \
104                 "%s(): " fmt "\n", __func__, ##args)
105
106 static uint16_t
107 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
108 {
109         unsigned i;
110         struct tpacket2_hdr *ppd;
111         struct rte_mbuf *mbuf;
112         uint8_t *pbuf;
113         struct pkt_rx_queue *pkt_q = queue;
114         uint16_t num_rx = 0;
115         unsigned long num_rx_bytes = 0;
116         unsigned int framecount, framenum;
117
118         if (unlikely(nb_pkts == 0))
119                 return 0;
120
121         /*
122          * Reads the given number of packets from the AF_PACKET socket one by
123          * one and copies the packet data into a newly allocated mbuf.
124          */
125         framecount = pkt_q->framecount;
126         framenum = pkt_q->framenum;
127         for (i = 0; i < nb_pkts; i++) {
128                 /* point at the next incoming frame */
129                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
130                 if ((ppd->tp_status & TP_STATUS_USER) == 0)
131                         break;
132
133                 /* allocate the next mbuf */
134                 mbuf = rte_pktmbuf_alloc(pkt_q->mb_pool);
135                 if (unlikely(mbuf == NULL))
136                         break;
137
138                 /* packet will fit in the mbuf, go ahead and receive it */
139                 rte_pktmbuf_pkt_len(mbuf) = rte_pktmbuf_data_len(mbuf) = ppd->tp_snaplen;
140                 pbuf = (uint8_t *) ppd + ppd->tp_mac;
141                 memcpy(rte_pktmbuf_mtod(mbuf, void *), pbuf, rte_pktmbuf_data_len(mbuf));
142
143                 /* check for vlan info */
144                 if (ppd->tp_status & TP_STATUS_VLAN_VALID) {
145                         mbuf->vlan_tci = ppd->tp_vlan_tci;
146                         mbuf->ol_flags |= (PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED);
147                 }
148
149                 /* release incoming frame and advance ring buffer */
150                 ppd->tp_status = TP_STATUS_KERNEL;
151                 if (++framenum >= framecount)
152                         framenum = 0;
153                 mbuf->port = pkt_q->in_port;
154
155                 /* account for the receive frame */
156                 bufs[i] = mbuf;
157                 num_rx++;
158                 num_rx_bytes += mbuf->pkt_len;
159         }
160         pkt_q->framenum = framenum;
161         pkt_q->rx_pkts += num_rx;
162         pkt_q->rx_bytes += num_rx_bytes;
163         return num_rx;
164 }
165
166 /*
167  * Callback to handle sending packets through a real NIC.
168  */
169 static uint16_t
170 eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
171 {
172         struct tpacket2_hdr *ppd;
173         struct rte_mbuf *mbuf;
174         uint8_t *pbuf;
175         unsigned int framecount, framenum;
176         struct pollfd pfd;
177         struct pkt_tx_queue *pkt_q = queue;
178         uint16_t num_tx = 0;
179         unsigned long num_tx_bytes = 0;
180         int i;
181
182         if (unlikely(nb_pkts == 0))
183                 return 0;
184
185         memset(&pfd, 0, sizeof(pfd));
186         pfd.fd = pkt_q->sockfd;
187         pfd.events = POLLOUT;
188         pfd.revents = 0;
189
190         framecount = pkt_q->framecount;
191         framenum = pkt_q->framenum;
192         ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
193         for (i = 0; i < nb_pkts; i++) {
194                 mbuf = *bufs++;
195
196                 /* drop oversized packets */
197                 if (mbuf->pkt_len > pkt_q->frame_data_size) {
198                         rte_pktmbuf_free(mbuf);
199                         continue;
200                 }
201
202                 /* insert vlan info if necessary */
203                 if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
204                         if (rte_vlan_insert(&mbuf)) {
205                                 rte_pktmbuf_free(mbuf);
206                                 continue;
207                         }
208                 }
209
210                 /* point at the next incoming frame */
211                 if ((ppd->tp_status != TP_STATUS_AVAILABLE) &&
212                     (poll(&pfd, 1, -1) < 0))
213                         break;
214
215                 /* copy the tx frame data */
216                 pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN -
217                         sizeof(struct sockaddr_ll);
218
219                 struct rte_mbuf *tmp_mbuf = mbuf;
220                 while (tmp_mbuf) {
221                         uint16_t data_len = rte_pktmbuf_data_len(tmp_mbuf);
222                         memcpy(pbuf, rte_pktmbuf_mtod(tmp_mbuf, void*), data_len);
223                         pbuf += data_len;
224                         tmp_mbuf = tmp_mbuf->next;
225                 }
226
227                 ppd->tp_len = mbuf->pkt_len;
228                 ppd->tp_snaplen = mbuf->pkt_len;
229
230                 /* release incoming frame and advance ring buffer */
231                 ppd->tp_status = TP_STATUS_SEND_REQUEST;
232                 if (++framenum >= framecount)
233                         framenum = 0;
234                 ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
235
236                 num_tx++;
237                 num_tx_bytes += mbuf->pkt_len;
238                 rte_pktmbuf_free(mbuf);
239         }
240
241         /* kick-off transmits */
242         if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1) {
243                 /* error sending -- no packets transmitted */
244                 num_tx = 0;
245                 num_tx_bytes = 0;
246         }
247
248         pkt_q->framenum = framenum;
249         pkt_q->tx_pkts += num_tx;
250         pkt_q->err_pkts += i - num_tx;
251         pkt_q->tx_bytes += num_tx_bytes;
252         return i;
253 }
254
255 static int
256 eth_dev_start(struct rte_eth_dev *dev)
257 {
258         dev->data->dev_link.link_status = ETH_LINK_UP;
259         return 0;
260 }
261
262 /*
263  * This function gets called when the current port gets stopped.
264  */
265 static void
266 eth_dev_stop(struct rte_eth_dev *dev)
267 {
268         unsigned i;
269         int sockfd;
270         struct pmd_internals *internals = dev->data->dev_private;
271
272         for (i = 0; i < internals->nb_queues; i++) {
273                 sockfd = internals->rx_queue[i].sockfd;
274                 if (sockfd != -1)
275                         close(sockfd);
276
277                 /* Prevent use after free in case tx fd == rx fd */
278                 if (sockfd != internals->tx_queue[i].sockfd) {
279                         sockfd = internals->tx_queue[i].sockfd;
280                         if (sockfd != -1)
281                                 close(sockfd);
282                 }
283
284                 internals->rx_queue[i].sockfd = -1;
285                 internals->tx_queue[i].sockfd = -1;
286         }
287
288         dev->data->dev_link.link_status = ETH_LINK_DOWN;
289 }
290
291 static int
292 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
293 {
294         return 0;
295 }
296
297 static void
298 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
299 {
300         struct pmd_internals *internals = dev->data->dev_private;
301
302         dev_info->if_index = internals->if_index;
303         dev_info->max_mac_addrs = 1;
304         dev_info->max_rx_pktlen = (uint32_t)ETH_FRAME_LEN;
305         dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
306         dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
307         dev_info->min_rx_bufsize = 0;
308 }
309
310 static int
311 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *igb_stats)
312 {
313         unsigned i, imax;
314         unsigned long rx_total = 0, tx_total = 0, tx_err_total = 0;
315         unsigned long rx_bytes_total = 0, tx_bytes_total = 0;
316         const struct pmd_internals *internal = dev->data->dev_private;
317
318         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
319                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
320         for (i = 0; i < imax; i++) {
321                 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
322                 igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;
323                 rx_total += igb_stats->q_ipackets[i];
324                 rx_bytes_total += igb_stats->q_ibytes[i];
325         }
326
327         imax = (internal->nb_queues < RTE_ETHDEV_QUEUE_STAT_CNTRS ?
328                 internal->nb_queues : RTE_ETHDEV_QUEUE_STAT_CNTRS);
329         for (i = 0; i < imax; i++) {
330                 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
331                 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
332                 igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes;
333                 tx_total += igb_stats->q_opackets[i];
334                 tx_err_total += igb_stats->q_errors[i];
335                 tx_bytes_total += igb_stats->q_obytes[i];
336         }
337
338         igb_stats->ipackets = rx_total;
339         igb_stats->ibytes = rx_bytes_total;
340         igb_stats->opackets = tx_total;
341         igb_stats->oerrors = tx_err_total;
342         igb_stats->obytes = tx_bytes_total;
343         return 0;
344 }
345
346 static void
347 eth_stats_reset(struct rte_eth_dev *dev)
348 {
349         unsigned i;
350         struct pmd_internals *internal = dev->data->dev_private;
351
352         for (i = 0; i < internal->nb_queues; i++) {
353                 internal->rx_queue[i].rx_pkts = 0;
354                 internal->rx_queue[i].rx_bytes = 0;
355         }
356
357         for (i = 0; i < internal->nb_queues; i++) {
358                 internal->tx_queue[i].tx_pkts = 0;
359                 internal->tx_queue[i].err_pkts = 0;
360                 internal->tx_queue[i].tx_bytes = 0;
361         }
362 }
363
364 static void
365 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
366 {
367 }
368
369 static void
370 eth_queue_release(void *q __rte_unused)
371 {
372 }
373
374 static int
375 eth_link_update(struct rte_eth_dev *dev __rte_unused,
376                 int wait_to_complete __rte_unused)
377 {
378         return 0;
379 }
380
381 static int
382 eth_rx_queue_setup(struct rte_eth_dev *dev,
383                    uint16_t rx_queue_id,
384                    uint16_t nb_rx_desc __rte_unused,
385                    unsigned int socket_id __rte_unused,
386                    const struct rte_eth_rxconf *rx_conf __rte_unused,
387                    struct rte_mempool *mb_pool)
388 {
389         struct pmd_internals *internals = dev->data->dev_private;
390         struct pkt_rx_queue *pkt_q = &internals->rx_queue[rx_queue_id];
391         unsigned int buf_size, data_size;
392
393         pkt_q->mb_pool = mb_pool;
394
395         /* Now get the space available for data in the mbuf */
396         buf_size = rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
397                 RTE_PKTMBUF_HEADROOM;
398         data_size = internals->req.tp_frame_size;
399         data_size -= TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
400
401         if (data_size > buf_size) {
402                 PMD_LOG(ERR,
403                         "%s: %d bytes will not fit in mbuf (%d bytes)",
404                         dev->device->name, data_size, buf_size);
405                 return -ENOMEM;
406         }
407
408         dev->data->rx_queues[rx_queue_id] = pkt_q;
409         pkt_q->in_port = dev->data->port_id;
410
411         return 0;
412 }
413
414 static int
415 eth_tx_queue_setup(struct rte_eth_dev *dev,
416                    uint16_t tx_queue_id,
417                    uint16_t nb_tx_desc __rte_unused,
418                    unsigned int socket_id __rte_unused,
419                    const struct rte_eth_txconf *tx_conf __rte_unused)
420 {
421
422         struct pmd_internals *internals = dev->data->dev_private;
423
424         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
425         return 0;
426 }
427
428 static int
429 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
430 {
431         struct pmd_internals *internals = dev->data->dev_private;
432         struct ifreq ifr = { .ifr_mtu = mtu };
433         int ret;
434         int s;
435         unsigned int data_size = internals->req.tp_frame_size -
436                                  TPACKET2_HDRLEN -
437                                  sizeof(struct sockaddr_ll);
438
439         if (mtu > data_size)
440                 return -EINVAL;
441
442         s = socket(PF_INET, SOCK_DGRAM, 0);
443         if (s < 0)
444                 return -EINVAL;
445
446         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", internals->if_name);
447         ret = ioctl(s, SIOCSIFMTU, &ifr);
448         close(s);
449
450         if (ret < 0)
451                 return -EINVAL;
452
453         return 0;
454 }
455
456 static void
457 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
458 {
459         struct ifreq ifr;
460         int s;
461
462         s = socket(PF_INET, SOCK_DGRAM, 0);
463         if (s < 0)
464                 return;
465
466         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", if_name);
467         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
468                 goto out;
469         ifr.ifr_flags &= mask;
470         ifr.ifr_flags |= flags;
471         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
472                 goto out;
473 out:
474         close(s);
475 }
476
477 static void
478 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
479 {
480         struct pmd_internals *internals = dev->data->dev_private;
481
482         eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
483 }
484
485 static void
486 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
487 {
488         struct pmd_internals *internals = dev->data->dev_private;
489
490         eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
491 }
492
493 static const struct eth_dev_ops ops = {
494         .dev_start = eth_dev_start,
495         .dev_stop = eth_dev_stop,
496         .dev_close = eth_dev_close,
497         .dev_configure = eth_dev_configure,
498         .dev_infos_get = eth_dev_info,
499         .mtu_set = eth_dev_mtu_set,
500         .promiscuous_enable = eth_dev_promiscuous_enable,
501         .promiscuous_disable = eth_dev_promiscuous_disable,
502         .rx_queue_setup = eth_rx_queue_setup,
503         .tx_queue_setup = eth_tx_queue_setup,
504         .rx_queue_release = eth_queue_release,
505         .tx_queue_release = eth_queue_release,
506         .link_update = eth_link_update,
507         .stats_get = eth_stats_get,
508         .stats_reset = eth_stats_reset,
509 };
510
511 /*
512  * Opens an AF_PACKET socket
513  */
514 static int
515 open_packet_iface(const char *key __rte_unused,
516                   const char *value __rte_unused,
517                   void *extra_args)
518 {
519         int *sockfd = extra_args;
520
521         /* Open an AF_PACKET socket... */
522         *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
523         if (*sockfd == -1) {
524                 PMD_LOG(ERR, "Could not open AF_PACKET socket");
525                 return -1;
526         }
527
528         return 0;
529 }
530
531 static struct rte_vdev_driver pmd_af_packet_drv;
532
533 static int
534 rte_pmd_init_internals(struct rte_vdev_device *dev,
535                        const int sockfd,
536                        const unsigned nb_queues,
537                        unsigned int blocksize,
538                        unsigned int blockcnt,
539                        unsigned int framesize,
540                        unsigned int framecnt,
541                        unsigned int qdisc_bypass,
542                        struct pmd_internals **internals,
543                        struct rte_eth_dev **eth_dev,
544                        struct rte_kvargs *kvlist)
545 {
546         const char *name = rte_vdev_device_name(dev);
547         const unsigned int numa_node = dev->device.numa_node;
548         struct rte_eth_dev_data *data = NULL;
549         struct rte_kvargs_pair *pair = NULL;
550         struct ifreq ifr;
551         size_t ifnamelen;
552         unsigned k_idx;
553         struct sockaddr_ll sockaddr;
554         struct tpacket_req *req;
555         struct pkt_rx_queue *rx_queue;
556         struct pkt_tx_queue *tx_queue;
557         int rc, tpver, discard;
558         int qsockfd = -1;
559         unsigned int i, q, rdsize;
560 #if defined(PACKET_FANOUT)
561         int fanout_arg;
562 #endif
563
564         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
565                 pair = &kvlist->pairs[k_idx];
566                 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL)
567                         break;
568         }
569         if (pair == NULL) {
570                 PMD_LOG(ERR,
571                         "%s: no interface specified for AF_PACKET ethdev",
572                         name);
573                 return -1;
574         }
575
576         PMD_LOG(INFO,
577                 "%s: creating AF_PACKET-backed ethdev on numa socket %u",
578                 name, numa_node);
579
580         *internals = rte_zmalloc_socket(name, sizeof(**internals),
581                                         0, numa_node);
582         if (*internals == NULL)
583                 return -1;
584
585         for (q = 0; q < nb_queues; q++) {
586                 (*internals)->rx_queue[q].map = MAP_FAILED;
587                 (*internals)->tx_queue[q].map = MAP_FAILED;
588         }
589
590         req = &((*internals)->req);
591
592         req->tp_block_size = blocksize;
593         req->tp_block_nr = blockcnt;
594         req->tp_frame_size = framesize;
595         req->tp_frame_nr = framecnt;
596
597         ifnamelen = strlen(pair->value);
598         if (ifnamelen < sizeof(ifr.ifr_name)) {
599                 memcpy(ifr.ifr_name, pair->value, ifnamelen);
600                 ifr.ifr_name[ifnamelen] = '\0';
601         } else {
602                 PMD_LOG(ERR,
603                         "%s: I/F name too long (%s)",
604                         name, pair->value);
605                 return -1;
606         }
607         if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
608                 PMD_LOG(ERR,
609                         "%s: ioctl failed (SIOCGIFINDEX)",
610                         name);
611                 return -1;
612         }
613         (*internals)->if_name = strdup(pair->value);
614         if ((*internals)->if_name == NULL)
615                 return -1;
616         (*internals)->if_index = ifr.ifr_ifindex;
617
618         if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
619                 PMD_LOG(ERR,
620                         "%s: ioctl failed (SIOCGIFHWADDR)",
621                         name);
622                 return -1;
623         }
624         memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
625
626         memset(&sockaddr, 0, sizeof(sockaddr));
627         sockaddr.sll_family = AF_PACKET;
628         sockaddr.sll_protocol = htons(ETH_P_ALL);
629         sockaddr.sll_ifindex = (*internals)->if_index;
630
631 #if defined(PACKET_FANOUT)
632         fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
633         fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
634 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
635         fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
636 #endif
637 #endif
638
639         for (q = 0; q < nb_queues; q++) {
640                 /* Open an AF_PACKET socket for this queue... */
641                 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
642                 if (qsockfd == -1) {
643                         PMD_LOG(ERR,
644                                 "%s: could not open AF_PACKET socket",
645                                 name);
646                         return -1;
647                 }
648
649                 tpver = TPACKET_V2;
650                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION,
651                                 &tpver, sizeof(tpver));
652                 if (rc == -1) {
653                         PMD_LOG(ERR,
654                                 "%s: could not set PACKET_VERSION on AF_PACKET socket for %s",
655                                 name, pair->value);
656                         goto error;
657                 }
658
659                 discard = 1;
660                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS,
661                                 &discard, sizeof(discard));
662                 if (rc == -1) {
663                         PMD_LOG(ERR,
664                                 "%s: could not set PACKET_LOSS on AF_PACKET socket for %s",
665                                 name, pair->value);
666                         goto error;
667                 }
668
669 #if defined(PACKET_QDISC_BYPASS)
670                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
671                                 &qdisc_bypass, sizeof(qdisc_bypass));
672                 if (rc == -1) {
673                         PMD_LOG(ERR,
674                                 "%s: could not set PACKET_QDISC_BYPASS on AF_PACKET socket for %s",
675                                 name, pair->value);
676                         goto error;
677                 }
678 #else
679                 RTE_SET_USED(qdisc_bypass);
680 #endif
681
682                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
683                 if (rc == -1) {
684                         PMD_LOG(ERR,
685                                 "%s: could not set PACKET_RX_RING on AF_PACKET socket for %s",
686                                 name, pair->value);
687                         goto error;
688                 }
689
690                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req));
691                 if (rc == -1) {
692                         PMD_LOG(ERR,
693                                 "%s: could not set PACKET_TX_RING on AF_PACKET "
694                                 "socket for %s", name, pair->value);
695                         goto error;
696                 }
697
698                 rx_queue = &((*internals)->rx_queue[q]);
699                 rx_queue->framecount = req->tp_frame_nr;
700
701                 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr,
702                                     PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
703                                     qsockfd, 0);
704                 if (rx_queue->map == MAP_FAILED) {
705                         PMD_LOG(ERR,
706                                 "%s: call to mmap failed on AF_PACKET socket for %s",
707                                 name, pair->value);
708                         goto error;
709                 }
710
711                 /* rdsize is same for both Tx and Rx */
712                 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd));
713
714                 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
715                 if (rx_queue->rd == NULL)
716                         goto error;
717                 for (i = 0; i < req->tp_frame_nr; ++i) {
718                         rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
719                         rx_queue->rd[i].iov_len = req->tp_frame_size;
720                 }
721                 rx_queue->sockfd = qsockfd;
722
723                 tx_queue = &((*internals)->tx_queue[q]);
724                 tx_queue->framecount = req->tp_frame_nr;
725                 tx_queue->frame_data_size = req->tp_frame_size;
726                 tx_queue->frame_data_size -= TPACKET2_HDRLEN -
727                         sizeof(struct sockaddr_ll);
728
729                 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
730
731                 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
732                 if (tx_queue->rd == NULL)
733                         goto error;
734                 for (i = 0; i < req->tp_frame_nr; ++i) {
735                         tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
736                         tx_queue->rd[i].iov_len = req->tp_frame_size;
737                 }
738                 tx_queue->sockfd = qsockfd;
739
740                 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr));
741                 if (rc == -1) {
742                         PMD_LOG(ERR,
743                                 "%s: could not bind AF_PACKET socket to %s",
744                                 name, pair->value);
745                         goto error;
746                 }
747
748 #if defined(PACKET_FANOUT)
749                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
750                                 &fanout_arg, sizeof(fanout_arg));
751                 if (rc == -1) {
752                         PMD_LOG(ERR,
753                                 "%s: could not set PACKET_FANOUT on AF_PACKET socket "
754                                 "for %s", name, pair->value);
755                         goto error;
756                 }
757 #endif
758         }
759
760         /* reserve an ethdev entry */
761         *eth_dev = rte_eth_vdev_allocate(dev, 0);
762         if (*eth_dev == NULL)
763                 goto error;
764
765         /*
766          * now put it all together
767          * - store queue data in internals,
768          * - store numa_node in eth_dev
769          * - point eth_dev_data to internals
770          * - and point eth_dev structure to new eth_dev_data structure
771          */
772
773         (*internals)->nb_queues = nb_queues;
774
775         data = (*eth_dev)->data;
776         data->dev_private = *internals;
777         data->nb_rx_queues = (uint16_t)nb_queues;
778         data->nb_tx_queues = (uint16_t)nb_queues;
779         data->dev_link = pmd_link;
780         data->mac_addrs = &(*internals)->eth_addr;
781
782         (*eth_dev)->dev_ops = &ops;
783
784         return 0;
785
786 error:
787         if (qsockfd != -1)
788                 close(qsockfd);
789         for (q = 0; q < nb_queues; q++) {
790                 munmap((*internals)->rx_queue[q].map,
791                        2 * req->tp_block_size * req->tp_block_nr);
792
793                 rte_free((*internals)->rx_queue[q].rd);
794                 rte_free((*internals)->tx_queue[q].rd);
795                 if (((*internals)->rx_queue[q].sockfd != 0) &&
796                         ((*internals)->rx_queue[q].sockfd != qsockfd))
797                         close((*internals)->rx_queue[q].sockfd);
798         }
799         free((*internals)->if_name);
800         rte_free(*internals);
801         return -1;
802 }
803
804 static int
805 rte_eth_from_packet(struct rte_vdev_device *dev,
806                     int const *sockfd,
807                     struct rte_kvargs *kvlist)
808 {
809         const char *name = rte_vdev_device_name(dev);
810         struct pmd_internals *internals = NULL;
811         struct rte_eth_dev *eth_dev = NULL;
812         struct rte_kvargs_pair *pair = NULL;
813         unsigned k_idx;
814         unsigned int blockcount;
815         unsigned int blocksize = DFLT_BLOCK_SIZE;
816         unsigned int framesize = DFLT_FRAME_SIZE;
817         unsigned int framecount = DFLT_FRAME_COUNT;
818         unsigned int qpairs = 1;
819         unsigned int qdisc_bypass = 1;
820
821         /* do some parameter checking */
822         if (*sockfd < 0)
823                 return -1;
824
825         /*
826          * Walk arguments for configurable settings
827          */
828         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
829                 pair = &kvlist->pairs[k_idx];
830                 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
831                         qpairs = atoi(pair->value);
832                         if (qpairs < 1 ||
833                             qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
834                                 PMD_LOG(ERR,
835                                         "%s: invalid qpairs value",
836                                         name);
837                                 return -1;
838                         }
839                         continue;
840                 }
841                 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
842                         blocksize = atoi(pair->value);
843                         if (!blocksize) {
844                                 PMD_LOG(ERR,
845                                         "%s: invalid blocksize value",
846                                         name);
847                                 return -1;
848                         }
849                         continue;
850                 }
851                 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
852                         framesize = atoi(pair->value);
853                         if (!framesize) {
854                                 PMD_LOG(ERR,
855                                         "%s: invalid framesize value",
856                                         name);
857                                 return -1;
858                         }
859                         continue;
860                 }
861                 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
862                         framecount = atoi(pair->value);
863                         if (!framecount) {
864                                 PMD_LOG(ERR,
865                                         "%s: invalid framecount value",
866                                         name);
867                                 return -1;
868                         }
869                         continue;
870                 }
871                 if (strstr(pair->key, ETH_AF_PACKET_QDISC_BYPASS_ARG) != NULL) {
872                         qdisc_bypass = atoi(pair->value);
873                         if (qdisc_bypass > 1) {
874                                 PMD_LOG(ERR,
875                                         "%s: invalid bypass value",
876                                         name);
877                                 return -1;
878                         }
879                         continue;
880                 }
881         }
882
883         if (framesize > blocksize) {
884                 PMD_LOG(ERR,
885                         "%s: AF_PACKET MMAP frame size exceeds block size!",
886                         name);
887                 return -1;
888         }
889
890         blockcount = framecount / (blocksize / framesize);
891         if (!blockcount) {
892                 PMD_LOG(ERR,
893                         "%s: invalid AF_PACKET MMAP parameters", name);
894                 return -1;
895         }
896
897         PMD_LOG(INFO, "%s: AF_PACKET MMAP parameters:", name);
898         PMD_LOG(INFO, "%s:\tblock size %d", name, blocksize);
899         PMD_LOG(INFO, "%s:\tblock count %d", name, blockcount);
900         PMD_LOG(INFO, "%s:\tframe size %d", name, framesize);
901         PMD_LOG(INFO, "%s:\tframe count %d", name, framecount);
902
903         if (rte_pmd_init_internals(dev, *sockfd, qpairs,
904                                    blocksize, blockcount,
905                                    framesize, framecount,
906                                    qdisc_bypass,
907                                    &internals, &eth_dev,
908                                    kvlist) < 0)
909                 return -1;
910
911         eth_dev->rx_pkt_burst = eth_af_packet_rx;
912         eth_dev->tx_pkt_burst = eth_af_packet_tx;
913
914         rte_eth_dev_probing_finish(eth_dev);
915         return 0;
916 }
917
918 static int
919 rte_pmd_af_packet_probe(struct rte_vdev_device *dev)
920 {
921         int ret = 0;
922         struct rte_kvargs *kvlist;
923         int sockfd = -1;
924         struct rte_eth_dev *eth_dev;
925         const char *name = rte_vdev_device_name(dev);
926
927         PMD_LOG(INFO, "Initializing pmd_af_packet for %s", name);
928
929         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
930                 eth_dev = rte_eth_dev_attach_secondary(name);
931                 if (!eth_dev) {
932                         PMD_LOG(ERR, "Failed to probe %s", name);
933                         return -1;
934                 }
935                 /* TODO: request info from primary to set up Rx and Tx */
936                 eth_dev->dev_ops = &ops;
937                 eth_dev->device = &dev->device;
938                 rte_eth_dev_probing_finish(eth_dev);
939                 return 0;
940         }
941
942         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
943         if (kvlist == NULL) {
944                 ret = -1;
945                 goto exit;
946         }
947
948         /*
949          * If iface argument is passed we open the NICs and use them for
950          * reading / writing
951          */
952         if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
953
954                 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
955                                          &open_packet_iface, &sockfd);
956                 if (ret < 0)
957                         goto exit;
958         }
959
960         if (dev->device.numa_node == SOCKET_ID_ANY)
961                 dev->device.numa_node = rte_socket_id();
962
963         ret = rte_eth_from_packet(dev, &sockfd, kvlist);
964         close(sockfd); /* no longer needed */
965
966 exit:
967         rte_kvargs_free(kvlist);
968         return ret;
969 }
970
971 static int
972 rte_pmd_af_packet_remove(struct rte_vdev_device *dev)
973 {
974         struct rte_eth_dev *eth_dev = NULL;
975         struct pmd_internals *internals;
976         unsigned q;
977
978         PMD_LOG(INFO, "Closing AF_PACKET ethdev on numa socket %u",
979                 rte_socket_id());
980
981         if (dev == NULL)
982                 return -1;
983
984         /* find the ethdev entry */
985         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
986         if (eth_dev == NULL)
987                 return -1;
988
989         /* mac_addrs must not be freed alone because part of dev_private */
990         eth_dev->data->mac_addrs = NULL;
991
992         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
993                 return rte_eth_dev_release_port(eth_dev);
994
995         internals = eth_dev->data->dev_private;
996         for (q = 0; q < internals->nb_queues; q++) {
997                 rte_free(internals->rx_queue[q].rd);
998                 rte_free(internals->tx_queue[q].rd);
999         }
1000         free(internals->if_name);
1001
1002         rte_eth_dev_release_port(eth_dev);
1003
1004         return 0;
1005 }
1006
1007 static struct rte_vdev_driver pmd_af_packet_drv = {
1008         .probe = rte_pmd_af_packet_probe,
1009         .remove = rte_pmd_af_packet_remove,
1010 };
1011
1012 RTE_PMD_REGISTER_VDEV(net_af_packet, pmd_af_packet_drv);
1013 RTE_PMD_REGISTER_ALIAS(net_af_packet, eth_af_packet);
1014 RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
1015         "iface=<string> "
1016         "qpairs=<int> "
1017         "blocksz=<int> "
1018         "framesz=<int> "
1019         "framecnt=<int> "
1020         "qdisc_bypass=<0|1>");
1021
1022 RTE_INIT(af_packet_init_log)
1023 {
1024         af_packet_logtype = rte_log_register("pmd.net.packet");
1025         if (af_packet_logtype >= 0)
1026                 rte_log_set_level(af_packet_logtype, RTE_LOG_NOTICE);
1027 }