net/af_packet: fix setting MTU decrements sockaddr twice
[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
438         if (mtu > data_size)
439                 return -EINVAL;
440
441         s = socket(PF_INET, SOCK_DGRAM, 0);
442         if (s < 0)
443                 return -EINVAL;
444
445         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", internals->if_name);
446         ret = ioctl(s, SIOCSIFMTU, &ifr);
447         close(s);
448
449         if (ret < 0)
450                 return -EINVAL;
451
452         return 0;
453 }
454
455 static void
456 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
457 {
458         struct ifreq ifr;
459         int s;
460
461         s = socket(PF_INET, SOCK_DGRAM, 0);
462         if (s < 0)
463                 return;
464
465         snprintf(ifr.ifr_name, IFNAMSIZ, "%s", if_name);
466         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
467                 goto out;
468         ifr.ifr_flags &= mask;
469         ifr.ifr_flags |= flags;
470         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
471                 goto out;
472 out:
473         close(s);
474 }
475
476 static void
477 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
478 {
479         struct pmd_internals *internals = dev->data->dev_private;
480
481         eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
482 }
483
484 static void
485 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
486 {
487         struct pmd_internals *internals = dev->data->dev_private;
488
489         eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
490 }
491
492 static const struct eth_dev_ops ops = {
493         .dev_start = eth_dev_start,
494         .dev_stop = eth_dev_stop,
495         .dev_close = eth_dev_close,
496         .dev_configure = eth_dev_configure,
497         .dev_infos_get = eth_dev_info,
498         .mtu_set = eth_dev_mtu_set,
499         .promiscuous_enable = eth_dev_promiscuous_enable,
500         .promiscuous_disable = eth_dev_promiscuous_disable,
501         .rx_queue_setup = eth_rx_queue_setup,
502         .tx_queue_setup = eth_tx_queue_setup,
503         .rx_queue_release = eth_queue_release,
504         .tx_queue_release = eth_queue_release,
505         .link_update = eth_link_update,
506         .stats_get = eth_stats_get,
507         .stats_reset = eth_stats_reset,
508 };
509
510 /*
511  * Opens an AF_PACKET socket
512  */
513 static int
514 open_packet_iface(const char *key __rte_unused,
515                   const char *value __rte_unused,
516                   void *extra_args)
517 {
518         int *sockfd = extra_args;
519
520         /* Open an AF_PACKET socket... */
521         *sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
522         if (*sockfd == -1) {
523                 PMD_LOG(ERR, "Could not open AF_PACKET socket");
524                 return -1;
525         }
526
527         return 0;
528 }
529
530 static struct rte_vdev_driver pmd_af_packet_drv;
531
532 static int
533 rte_pmd_init_internals(struct rte_vdev_device *dev,
534                        const int sockfd,
535                        const unsigned nb_queues,
536                        unsigned int blocksize,
537                        unsigned int blockcnt,
538                        unsigned int framesize,
539                        unsigned int framecnt,
540                        unsigned int qdisc_bypass,
541                        struct pmd_internals **internals,
542                        struct rte_eth_dev **eth_dev,
543                        struct rte_kvargs *kvlist)
544 {
545         const char *name = rte_vdev_device_name(dev);
546         const unsigned int numa_node = dev->device.numa_node;
547         struct rte_eth_dev_data *data = NULL;
548         struct rte_kvargs_pair *pair = NULL;
549         struct ifreq ifr;
550         size_t ifnamelen;
551         unsigned k_idx;
552         struct sockaddr_ll sockaddr;
553         struct tpacket_req *req;
554         struct pkt_rx_queue *rx_queue;
555         struct pkt_tx_queue *tx_queue;
556         int rc, tpver, discard;
557         int qsockfd = -1;
558         unsigned int i, q, rdsize;
559 #if defined(PACKET_FANOUT)
560         int fanout_arg;
561 #endif
562
563         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
564                 pair = &kvlist->pairs[k_idx];
565                 if (strstr(pair->key, ETH_AF_PACKET_IFACE_ARG) != NULL)
566                         break;
567         }
568         if (pair == NULL) {
569                 PMD_LOG(ERR,
570                         "%s: no interface specified for AF_PACKET ethdev",
571                         name);
572                 return -1;
573         }
574
575         PMD_LOG(INFO,
576                 "%s: creating AF_PACKET-backed ethdev on numa socket %u",
577                 name, numa_node);
578
579         *internals = rte_zmalloc_socket(name, sizeof(**internals),
580                                         0, numa_node);
581         if (*internals == NULL)
582                 return -1;
583
584         for (q = 0; q < nb_queues; q++) {
585                 (*internals)->rx_queue[q].map = MAP_FAILED;
586                 (*internals)->tx_queue[q].map = MAP_FAILED;
587         }
588
589         req = &((*internals)->req);
590
591         req->tp_block_size = blocksize;
592         req->tp_block_nr = blockcnt;
593         req->tp_frame_size = framesize;
594         req->tp_frame_nr = framecnt;
595
596         ifnamelen = strlen(pair->value);
597         if (ifnamelen < sizeof(ifr.ifr_name)) {
598                 memcpy(ifr.ifr_name, pair->value, ifnamelen);
599                 ifr.ifr_name[ifnamelen] = '\0';
600         } else {
601                 PMD_LOG(ERR,
602                         "%s: I/F name too long (%s)",
603                         name, pair->value);
604                 return -1;
605         }
606         if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
607                 PMD_LOG(ERR,
608                         "%s: ioctl failed (SIOCGIFINDEX)",
609                         name);
610                 return -1;
611         }
612         (*internals)->if_name = strdup(pair->value);
613         if ((*internals)->if_name == NULL)
614                 return -1;
615         (*internals)->if_index = ifr.ifr_ifindex;
616
617         if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
618                 PMD_LOG(ERR,
619                         "%s: ioctl failed (SIOCGIFHWADDR)",
620                         name);
621                 return -1;
622         }
623         memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
624
625         memset(&sockaddr, 0, sizeof(sockaddr));
626         sockaddr.sll_family = AF_PACKET;
627         sockaddr.sll_protocol = htons(ETH_P_ALL);
628         sockaddr.sll_ifindex = (*internals)->if_index;
629
630 #if defined(PACKET_FANOUT)
631         fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
632         fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
633 #if defined(PACKET_FANOUT_FLAG_ROLLOVER)
634         fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
635 #endif
636 #endif
637
638         for (q = 0; q < nb_queues; q++) {
639                 /* Open an AF_PACKET socket for this queue... */
640                 qsockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
641                 if (qsockfd == -1) {
642                         PMD_LOG(ERR,
643                                 "%s: could not open AF_PACKET socket",
644                                 name);
645                         return -1;
646                 }
647
648                 tpver = TPACKET_V2;
649                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_VERSION,
650                                 &tpver, sizeof(tpver));
651                 if (rc == -1) {
652                         PMD_LOG(ERR,
653                                 "%s: could not set PACKET_VERSION on AF_PACKET socket for %s",
654                                 name, pair->value);
655                         goto error;
656                 }
657
658                 discard = 1;
659                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_LOSS,
660                                 &discard, sizeof(discard));
661                 if (rc == -1) {
662                         PMD_LOG(ERR,
663                                 "%s: could not set PACKET_LOSS on AF_PACKET socket for %s",
664                                 name, pair->value);
665                         goto error;
666                 }
667
668 #if defined(PACKET_QDISC_BYPASS)
669                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
670                                 &qdisc_bypass, sizeof(qdisc_bypass));
671                 if (rc == -1) {
672                         PMD_LOG(ERR,
673                                 "%s: could not set PACKET_QDISC_BYPASS on AF_PACKET socket for %s",
674                                 name, pair->value);
675                         goto error;
676                 }
677 #else
678                 RTE_SET_USED(qdisc_bypass);
679 #endif
680
681                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
682                 if (rc == -1) {
683                         PMD_LOG(ERR,
684                                 "%s: could not set PACKET_RX_RING on AF_PACKET socket for %s",
685                                 name, pair->value);
686                         goto error;
687                 }
688
689                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_TX_RING, req, sizeof(*req));
690                 if (rc == -1) {
691                         PMD_LOG(ERR,
692                                 "%s: could not set PACKET_TX_RING on AF_PACKET "
693                                 "socket for %s", name, pair->value);
694                         goto error;
695                 }
696
697                 rx_queue = &((*internals)->rx_queue[q]);
698                 rx_queue->framecount = req->tp_frame_nr;
699
700                 rx_queue->map = mmap(NULL, 2 * req->tp_block_size * req->tp_block_nr,
701                                     PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
702                                     qsockfd, 0);
703                 if (rx_queue->map == MAP_FAILED) {
704                         PMD_LOG(ERR,
705                                 "%s: call to mmap failed on AF_PACKET socket for %s",
706                                 name, pair->value);
707                         goto error;
708                 }
709
710                 /* rdsize is same for both Tx and Rx */
711                 rdsize = req->tp_frame_nr * sizeof(*(rx_queue->rd));
712
713                 rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
714                 if (rx_queue->rd == NULL)
715                         goto error;
716                 for (i = 0; i < req->tp_frame_nr; ++i) {
717                         rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
718                         rx_queue->rd[i].iov_len = req->tp_frame_size;
719                 }
720                 rx_queue->sockfd = qsockfd;
721
722                 tx_queue = &((*internals)->tx_queue[q]);
723                 tx_queue->framecount = req->tp_frame_nr;
724                 tx_queue->frame_data_size = req->tp_frame_size;
725                 tx_queue->frame_data_size -= TPACKET2_HDRLEN -
726                         sizeof(struct sockaddr_ll);
727
728                 tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
729
730                 tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
731                 if (tx_queue->rd == NULL)
732                         goto error;
733                 for (i = 0; i < req->tp_frame_nr; ++i) {
734                         tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
735                         tx_queue->rd[i].iov_len = req->tp_frame_size;
736                 }
737                 tx_queue->sockfd = qsockfd;
738
739                 rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, sizeof(sockaddr));
740                 if (rc == -1) {
741                         PMD_LOG(ERR,
742                                 "%s: could not bind AF_PACKET socket to %s",
743                                 name, pair->value);
744                         goto error;
745                 }
746
747 #if defined(PACKET_FANOUT)
748                 rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
749                                 &fanout_arg, sizeof(fanout_arg));
750                 if (rc == -1) {
751                         PMD_LOG(ERR,
752                                 "%s: could not set PACKET_FANOUT on AF_PACKET socket "
753                                 "for %s", name, pair->value);
754                         goto error;
755                 }
756 #endif
757         }
758
759         /* reserve an ethdev entry */
760         *eth_dev = rte_eth_vdev_allocate(dev, 0);
761         if (*eth_dev == NULL)
762                 goto error;
763
764         /*
765          * now put it all together
766          * - store queue data in internals,
767          * - store numa_node in eth_dev
768          * - point eth_dev_data to internals
769          * - and point eth_dev structure to new eth_dev_data structure
770          */
771
772         (*internals)->nb_queues = nb_queues;
773
774         data = (*eth_dev)->data;
775         data->dev_private = *internals;
776         data->nb_rx_queues = (uint16_t)nb_queues;
777         data->nb_tx_queues = (uint16_t)nb_queues;
778         data->dev_link = pmd_link;
779         data->mac_addrs = &(*internals)->eth_addr;
780
781         (*eth_dev)->dev_ops = &ops;
782
783         return 0;
784
785 error:
786         if (qsockfd != -1)
787                 close(qsockfd);
788         for (q = 0; q < nb_queues; q++) {
789                 munmap((*internals)->rx_queue[q].map,
790                        2 * req->tp_block_size * req->tp_block_nr);
791
792                 rte_free((*internals)->rx_queue[q].rd);
793                 rte_free((*internals)->tx_queue[q].rd);
794                 if (((*internals)->rx_queue[q].sockfd != 0) &&
795                         ((*internals)->rx_queue[q].sockfd != qsockfd))
796                         close((*internals)->rx_queue[q].sockfd);
797         }
798         free((*internals)->if_name);
799         rte_free(*internals);
800         return -1;
801 }
802
803 static int
804 rte_eth_from_packet(struct rte_vdev_device *dev,
805                     int const *sockfd,
806                     struct rte_kvargs *kvlist)
807 {
808         const char *name = rte_vdev_device_name(dev);
809         struct pmd_internals *internals = NULL;
810         struct rte_eth_dev *eth_dev = NULL;
811         struct rte_kvargs_pair *pair = NULL;
812         unsigned k_idx;
813         unsigned int blockcount;
814         unsigned int blocksize = DFLT_BLOCK_SIZE;
815         unsigned int framesize = DFLT_FRAME_SIZE;
816         unsigned int framecount = DFLT_FRAME_COUNT;
817         unsigned int qpairs = 1;
818         unsigned int qdisc_bypass = 1;
819
820         /* do some parameter checking */
821         if (*sockfd < 0)
822                 return -1;
823
824         /*
825          * Walk arguments for configurable settings
826          */
827         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
828                 pair = &kvlist->pairs[k_idx];
829                 if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
830                         qpairs = atoi(pair->value);
831                         if (qpairs < 1 ||
832                             qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
833                                 PMD_LOG(ERR,
834                                         "%s: invalid qpairs value",
835                                         name);
836                                 return -1;
837                         }
838                         continue;
839                 }
840                 if (strstr(pair->key, ETH_AF_PACKET_BLOCKSIZE_ARG) != NULL) {
841                         blocksize = atoi(pair->value);
842                         if (!blocksize) {
843                                 PMD_LOG(ERR,
844                                         "%s: invalid blocksize value",
845                                         name);
846                                 return -1;
847                         }
848                         continue;
849                 }
850                 if (strstr(pair->key, ETH_AF_PACKET_FRAMESIZE_ARG) != NULL) {
851                         framesize = atoi(pair->value);
852                         if (!framesize) {
853                                 PMD_LOG(ERR,
854                                         "%s: invalid framesize value",
855                                         name);
856                                 return -1;
857                         }
858                         continue;
859                 }
860                 if (strstr(pair->key, ETH_AF_PACKET_FRAMECOUNT_ARG) != NULL) {
861                         framecount = atoi(pair->value);
862                         if (!framecount) {
863                                 PMD_LOG(ERR,
864                                         "%s: invalid framecount value",
865                                         name);
866                                 return -1;
867                         }
868                         continue;
869                 }
870                 if (strstr(pair->key, ETH_AF_PACKET_QDISC_BYPASS_ARG) != NULL) {
871                         qdisc_bypass = atoi(pair->value);
872                         if (qdisc_bypass > 1) {
873                                 PMD_LOG(ERR,
874                                         "%s: invalid bypass value",
875                                         name);
876                                 return -1;
877                         }
878                         continue;
879                 }
880         }
881
882         if (framesize > blocksize) {
883                 PMD_LOG(ERR,
884                         "%s: AF_PACKET MMAP frame size exceeds block size!",
885                         name);
886                 return -1;
887         }
888
889         blockcount = framecount / (blocksize / framesize);
890         if (!blockcount) {
891                 PMD_LOG(ERR,
892                         "%s: invalid AF_PACKET MMAP parameters", name);
893                 return -1;
894         }
895
896         PMD_LOG(INFO, "%s: AF_PACKET MMAP parameters:", name);
897         PMD_LOG(INFO, "%s:\tblock size %d", name, blocksize);
898         PMD_LOG(INFO, "%s:\tblock count %d", name, blockcount);
899         PMD_LOG(INFO, "%s:\tframe size %d", name, framesize);
900         PMD_LOG(INFO, "%s:\tframe count %d", name, framecount);
901
902         if (rte_pmd_init_internals(dev, *sockfd, qpairs,
903                                    blocksize, blockcount,
904                                    framesize, framecount,
905                                    qdisc_bypass,
906                                    &internals, &eth_dev,
907                                    kvlist) < 0)
908                 return -1;
909
910         eth_dev->rx_pkt_burst = eth_af_packet_rx;
911         eth_dev->tx_pkt_burst = eth_af_packet_tx;
912
913         rte_eth_dev_probing_finish(eth_dev);
914         return 0;
915 }
916
917 static int
918 rte_pmd_af_packet_probe(struct rte_vdev_device *dev)
919 {
920         int ret = 0;
921         struct rte_kvargs *kvlist;
922         int sockfd = -1;
923         struct rte_eth_dev *eth_dev;
924         const char *name = rte_vdev_device_name(dev);
925
926         PMD_LOG(INFO, "Initializing pmd_af_packet for %s", name);
927
928         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
929                 eth_dev = rte_eth_dev_attach_secondary(name);
930                 if (!eth_dev) {
931                         PMD_LOG(ERR, "Failed to probe %s", name);
932                         return -1;
933                 }
934                 /* TODO: request info from primary to set up Rx and Tx */
935                 eth_dev->dev_ops = &ops;
936                 eth_dev->device = &dev->device;
937                 rte_eth_dev_probing_finish(eth_dev);
938                 return 0;
939         }
940
941         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
942         if (kvlist == NULL) {
943                 ret = -1;
944                 goto exit;
945         }
946
947         /*
948          * If iface argument is passed we open the NICs and use them for
949          * reading / writing
950          */
951         if (rte_kvargs_count(kvlist, ETH_AF_PACKET_IFACE_ARG) == 1) {
952
953                 ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
954                                          &open_packet_iface, &sockfd);
955                 if (ret < 0)
956                         goto exit;
957         }
958
959         if (dev->device.numa_node == SOCKET_ID_ANY)
960                 dev->device.numa_node = rte_socket_id();
961
962         ret = rte_eth_from_packet(dev, &sockfd, kvlist);
963         close(sockfd); /* no longer needed */
964
965 exit:
966         rte_kvargs_free(kvlist);
967         return ret;
968 }
969
970 static int
971 rte_pmd_af_packet_remove(struct rte_vdev_device *dev)
972 {
973         struct rte_eth_dev *eth_dev = NULL;
974         struct pmd_internals *internals;
975         unsigned q;
976
977         PMD_LOG(INFO, "Closing AF_PACKET ethdev on numa socket %u",
978                 rte_socket_id());
979
980         if (dev == NULL)
981                 return -1;
982
983         /* find the ethdev entry */
984         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
985         if (eth_dev == NULL)
986                 return -1;
987
988         /* mac_addrs must not be freed alone because part of dev_private */
989         eth_dev->data->mac_addrs = NULL;
990
991         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
992                 return rte_eth_dev_release_port(eth_dev);
993
994         internals = eth_dev->data->dev_private;
995         for (q = 0; q < internals->nb_queues; q++) {
996                 rte_free(internals->rx_queue[q].rd);
997                 rte_free(internals->tx_queue[q].rd);
998         }
999         free(internals->if_name);
1000
1001         rte_eth_dev_release_port(eth_dev);
1002
1003         return 0;
1004 }
1005
1006 static struct rte_vdev_driver pmd_af_packet_drv = {
1007         .probe = rte_pmd_af_packet_probe,
1008         .remove = rte_pmd_af_packet_remove,
1009 };
1010
1011 RTE_PMD_REGISTER_VDEV(net_af_packet, pmd_af_packet_drv);
1012 RTE_PMD_REGISTER_ALIAS(net_af_packet, eth_af_packet);
1013 RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
1014         "iface=<string> "
1015         "qpairs=<int> "
1016         "blocksz=<int> "
1017         "framesz=<int> "
1018         "framecnt=<int> "
1019         "qdisc_bypass=<0|1>");
1020
1021 RTE_INIT(af_packet_init_log)
1022 {
1023         af_packet_logtype = rte_log_register("pmd.net.packet");
1024         if (af_packet_logtype >= 0)
1025                 rte_log_set_level(af_packet_logtype, RTE_LOG_NOTICE);
1026 }