port: fix pcap sink parameter check
[dpdk.git] / lib / librte_port / rte_port_source_sink.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdint.h>
34 #include <string.h>
35
36 #include <rte_mbuf.h>
37 #include <rte_mempool.h>
38 #include <rte_malloc.h>
39
40 #include <rte_memcpy.h>
41
42 #ifdef RTE_NEXT_ABI
43
44 #ifdef RTE_PORT_PCAP
45 #include <rte_ether.h>
46 #include <pcap.h>
47 #endif
48
49 #else
50 #undef RTE_PORT_PCAP
51 #endif
52
53 #include "rte_port_source_sink.h"
54
55 /*
56  * Port SOURCE
57  */
58 #ifdef RTE_PORT_STATS_COLLECT
59
60 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val) \
61         port->stats.n_pkts_in += val
62 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val) \
63         port->stats.n_pkts_drop += val
64
65 #else
66
67 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val)
68 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val)
69
70 #endif
71
72 struct rte_port_source {
73         struct rte_port_in_stats stats;
74
75         struct rte_mempool *mempool;
76
77         /* PCAP buffers and indices */
78         uint8_t **pkts;
79         uint8_t *pkt_buff;
80         uint32_t *pkt_len;
81         uint32_t n_pkts;
82         uint32_t pkt_index;
83 };
84
85 #ifdef RTE_NEXT_ABI
86
87 #ifdef RTE_PORT_PCAP
88
89 static int
90 pcap_source_load(struct rte_port_source *port,
91                 const char *file_name,
92                 uint32_t n_bytes_per_pkt,
93                 int socket_id)
94 {
95         uint32_t n_pkts = 0;
96         uint32_t i;
97         uint32_t *pkt_len_aligns = NULL;
98         size_t total_buff_len = 0;
99         pcap_t *pcap_handle;
100         char pcap_errbuf[PCAP_ERRBUF_SIZE];
101         uint32_t max_len;
102         struct pcap_pkthdr pcap_hdr;
103         const uint8_t *pkt;
104         uint8_t *buff = NULL;
105         uint32_t pktmbuf_maxlen = (uint32_t)
106                         (rte_pktmbuf_data_room_size(port->mempool) -
107                         RTE_PKTMBUF_HEADROOM);
108
109         if (n_bytes_per_pkt == 0)
110                 max_len = pktmbuf_maxlen;
111         else
112                 max_len = RTE_MIN(n_bytes_per_pkt, pktmbuf_maxlen);
113
114         /* first time open, get packet number */
115         pcap_handle = pcap_open_offline(file_name, pcap_errbuf);
116         if (pcap_handle == NULL) {
117                 RTE_LOG(ERR, PORT, "Failed to open pcap file "
118                         "'%s' for reading\n", file_name);
119                 goto error_exit;
120         }
121
122         while ((pkt = pcap_next(pcap_handle, &pcap_hdr)) != NULL)
123                 n_pkts++;
124
125         pcap_close(pcap_handle);
126
127         port->pkt_len = rte_zmalloc_socket("PCAP",
128                 (sizeof(*port->pkt_len) * n_pkts), 0, socket_id);
129         if (port->pkt_len == NULL) {
130                 RTE_LOG(ERR, PORT, "No enough memory\n");
131                 goto error_exit;
132         }
133
134         pkt_len_aligns = rte_malloc("PCAP",
135                 (sizeof(*pkt_len_aligns) * n_pkts), 0);
136         if (pkt_len_aligns == NULL) {
137                 RTE_LOG(ERR, PORT, "No enough memory\n");
138                 goto error_exit;
139         }
140
141         port->pkts = rte_zmalloc_socket("PCAP",
142                 (sizeof(*port->pkts) * n_pkts), 0, socket_id);
143         if (port->pkts == NULL) {
144                 RTE_LOG(ERR, PORT, "No enough memory\n");
145                 goto error_exit;
146         }
147
148         /* open 2nd time, get pkt_len */
149         pcap_handle = pcap_open_offline(file_name, pcap_errbuf);
150         if (pcap_handle == NULL) {
151                 RTE_LOG(ERR, PORT, "Failed to open pcap file "
152                         "'%s' for reading\n", file_name);
153                 goto error_exit;
154         }
155
156         for (i = 0; i < n_pkts; i++) {
157                 pkt = pcap_next(pcap_handle, &pcap_hdr);
158                 port->pkt_len[i] = RTE_MIN(max_len, pcap_hdr.len);
159                 pkt_len_aligns[i] = RTE_CACHE_LINE_ROUNDUP(
160                         port->pkt_len[i]);
161                 total_buff_len += pkt_len_aligns[i];
162         }
163
164         pcap_close(pcap_handle);
165
166         /* allocate a big trunk of data for pcap file load */
167         buff = rte_zmalloc_socket("PCAP",
168                 total_buff_len, 0, socket_id);
169         if (buff == NULL) {
170                 RTE_LOG(ERR, PORT, "No enough memory\n");
171                 goto error_exit;
172         }
173
174         port->pkt_buff = buff;
175
176         /* open file one last time to copy the pkt content */
177         pcap_handle = pcap_open_offline(file_name, pcap_errbuf);
178         if (pcap_handle == NULL) {
179                 RTE_LOG(ERR, PORT, "Failed to open pcap file "
180                         "'%s' for reading\n", file_name);
181                 goto error_exit;
182         }
183
184         for (i = 0; i < n_pkts; i++) {
185                 pkt = pcap_next(pcap_handle, &pcap_hdr);
186                 rte_memcpy(buff, pkt, port->pkt_len[i]);
187                 port->pkts[i] = buff;
188                 buff += pkt_len_aligns[i];
189         }
190
191         pcap_close(pcap_handle);
192
193         port->n_pkts = n_pkts;
194
195         rte_free(pkt_len_aligns);
196
197         RTE_LOG(INFO, PORT, "Successfully load pcap file "
198                 "'%s' with %u pkts\n",
199                 file_name, port->n_pkts);
200
201         return 0;
202
203 error_exit:
204         if (pkt_len_aligns)
205                 rte_free(pkt_len_aligns);
206         if (port->pkt_len)
207                 rte_free(port->pkt_len);
208         if (port->pkts)
209                 rte_free(port->pkts);
210         if (port->pkt_buff)
211                 rte_free(port->pkt_buff);
212
213         return -1;
214 }
215
216 #define PCAP_SOURCE_LOAD(port, file_name, n_bytes, socket_id)   \
217         pcap_source_load(port, file_name, n_bytes, socket_id)
218
219 #else /* RTE_PORT_PCAP */
220
221 #define PCAP_SOURCE_LOAD(port, file_name, n_bytes, socket_id)   \
222 ({                                                              \
223         int _ret = 0;                                           \
224                                                                 \
225         if (file_name) {                                        \
226                 RTE_LOG(ERR, PORT, "Source port field "         \
227                         "\"file_name\" is not NULL.\n");        \
228                 _ret = -1;                                      \
229         }                                                       \
230                                                                 \
231         _ret;                                                   \
232 })
233
234 #endif /* RTE_PORT_PCAP */
235
236 #endif /* RTE_NEXT_ABI */
237
238 static void *
239 rte_port_source_create(void *params, int socket_id)
240 {
241         struct rte_port_source_params *p =
242                         (struct rte_port_source_params *) params;
243         struct rte_port_source *port;
244
245         /* Check input arguments*/
246         if ((p == NULL) || (p->mempool == NULL)) {
247                 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
248                 return NULL;
249         }
250
251         /* Memory allocation */
252         port = rte_zmalloc_socket("PORT", sizeof(*port),
253                         RTE_CACHE_LINE_SIZE, socket_id);
254         if (port == NULL) {
255                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
256                 return NULL;
257         }
258
259         /* Initialization */
260         port->mempool = (struct rte_mempool *) p->mempool;
261
262 #ifdef RTE_NEXT_ABI
263
264         if (p->file_name) {
265                 int status = PCAP_SOURCE_LOAD(port, p->file_name,
266                         p->n_bytes_per_pkt, socket_id);
267
268                 if (status < 0) {
269                         rte_free(port);
270                         port = NULL;
271                 }
272         }
273
274 #endif
275
276         return port;
277 }
278
279 static int
280 rte_port_source_free(void *port)
281 {
282         struct rte_port_source *p =
283                         (struct rte_port_source *)port;
284
285         /* Check input parameters */
286         if (p == NULL)
287                 return 0;
288
289 #ifdef RTE_NEXT_ABI
290
291         if (p->pkt_len)
292                 rte_free(p->pkt_len);
293         if (p->pkts)
294                 rte_free(p->pkts);
295         if (p->pkt_buff)
296                 rte_free(p->pkt_buff);
297 #endif
298
299         rte_free(p);
300
301         return 0;
302 }
303
304 static int
305 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
306 {
307         struct rte_port_source *p = (struct rte_port_source *) port;
308         uint32_t i;
309
310         if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
311                 return 0;
312
313         for (i = 0; i < n_pkts; i++) {
314                 rte_mbuf_refcnt_set(pkts[i], 1);
315                 rte_pktmbuf_reset(pkts[i]);
316         }
317
318 #ifdef RTE_NEXT_ABI
319
320         if (p->pkt_buff != NULL) {
321                 for (i = 0; i < n_pkts; i++) {
322                         uint8_t *pkt_data = rte_pktmbuf_mtod(pkts[i],
323                                 uint8_t *);
324
325                         rte_memcpy(pkt_data, p->pkts[p->pkt_index],
326                                         p->pkt_len[p->pkt_index]);
327                         pkts[i]->data_len = p->pkt_len[p->pkt_index];
328                         pkts[i]->pkt_len = pkts[i]->data_len;
329
330                         p->pkt_index++;
331                         if (p->pkt_index >= p->n_pkts)
332                                 p->pkt_index = 0;
333                 }
334         }
335
336 #endif
337
338         RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts);
339
340         return n_pkts;
341 }
342
343 static int
344 rte_port_source_stats_read(void *port,
345                 struct rte_port_in_stats *stats, int clear)
346 {
347         struct rte_port_source *p =
348                 (struct rte_port_source *) port;
349
350         if (stats != NULL)
351                 memcpy(stats, &p->stats, sizeof(p->stats));
352
353         if (clear)
354                 memset(&p->stats, 0, sizeof(p->stats));
355
356         return 0;
357 }
358
359 /*
360  * Port SINK
361  */
362 #ifdef RTE_PORT_STATS_COLLECT
363
364 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val) \
365         (port->stats.n_pkts_in += val)
366 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val) \
367         (port->stats.n_pkts_drop += val)
368
369 #else
370
371 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val)
372 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val)
373
374 #endif
375
376 struct rte_port_sink {
377         struct rte_port_out_stats stats;
378
379         /* PCAP dumper handle and pkts number */
380         void *dumper;
381         uint32_t max_pkts;
382         uint32_t pkt_index;
383         uint32_t dump_finish;
384 };
385
386 #ifdef RTE_PORT_PCAP
387
388 static int
389 pcap_sink_open(struct rte_port_sink *port,
390         const char *file_name,
391         uint32_t max_n_pkts)
392 {
393         pcap_t *tx_pcap;
394         pcap_dumper_t *pcap_dumper;
395
396         /** Open a dead pcap handler for opening dumper file */
397         tx_pcap = pcap_open_dead(DLT_EN10MB, 65535);
398         if (tx_pcap == NULL) {
399                 RTE_LOG(ERR, PORT, "Cannot open pcap dead handler\n");
400                 return -1;
401         }
402
403         /* The dumper is created using the previous pcap_t reference */
404         pcap_dumper = pcap_dump_open(tx_pcap, file_name);
405         if (pcap_dumper == NULL) {
406                 RTE_LOG(ERR, PORT, "Failed to open pcap file "
407                         "\"%s\" for writing\n", file_name);
408                 return -1;
409         }
410
411         port->dumper = pcap_dumper;
412         port->max_pkts = max_n_pkts;
413         port->pkt_index = 0;
414         port->dump_finish = 0;
415
416         RTE_LOG(INFO, PORT, "Ready to dump packets to file \"%s\"\n",
417                 file_name);
418
419         return 0;
420 }
421
422 uint8_t jumbo_pkt_buf[ETHER_MAX_JUMBO_FRAME_LEN];
423
424 /**
425  * Dump a packet to PCAP dumper
426  *
427  * @param p
428  *   Handle to sink port
429  * @param mbuf
430  *   Handle to mbuf structure holding the packet
431  */
432 static void
433 pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf)
434 {
435         uint8_t *pcap_dumper = (uint8_t *)(port->dumper);
436         struct pcap_pkthdr pcap_hdr;
437         uint8_t *pkt;
438
439         /* Maximum num packets already reached */
440         if (port->dump_finish)
441                 return;
442
443         pkt = rte_pktmbuf_mtod(mbuf, uint8_t *);
444
445         pcap_hdr.len = mbuf->pkt_len;
446         pcap_hdr.caplen = pcap_hdr.len;
447         gettimeofday(&(pcap_hdr.ts), NULL);
448
449         if (mbuf->nb_segs > 1) {
450                 struct rte_mbuf *jumbo_mbuf;
451                 uint32_t pkt_index = 0;
452
453                 /* if packet size longer than ETHER_MAX_JUMBO_FRAME_LEN,
454                  * ignore it.
455                  */
456                 if (mbuf->pkt_len > ETHER_MAX_JUMBO_FRAME_LEN)
457                         return;
458
459                 for (jumbo_mbuf = mbuf; jumbo_mbuf != NULL;
460                                 jumbo_mbuf = jumbo_mbuf->next) {
461                         rte_memcpy(&jumbo_pkt_buf[pkt_index],
462                                 rte_pktmbuf_mtod(jumbo_mbuf, uint8_t *),
463                                 jumbo_mbuf->data_len);
464                         pkt_index += jumbo_mbuf->data_len;
465                 }
466
467                 jumbo_pkt_buf[pkt_index] = '\0';
468
469                 pkt = jumbo_pkt_buf;
470         }
471
472         pcap_dump(pcap_dumper, &pcap_hdr, pkt);
473
474         port->pkt_index++;
475
476         if ((port->max_pkts != 0) && (port->pkt_index >= port->max_pkts)) {
477                 port->dump_finish = 1;
478                 RTE_LOG(INFO, PORT, "Dumped %u packets to file\n",
479                                 port->pkt_index);
480         }
481
482 }
483
484 /**
485  * Flush pcap dumper
486  *
487  * @param dumper
488  *   Handle to pcap dumper
489  */
490
491 static void
492 pcap_sink_flush_pkt(void *dumper)
493 {
494         pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
495
496         pcap_dump_flush(pcap_dumper);
497 }
498
499 /**
500  * Close a PCAP dumper handle
501  *
502  * @param dumper
503  *   Handle to pcap dumper
504  */
505 static void
506 pcap_sink_close(void *dumper)
507 {
508         pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
509
510         pcap_dump_close(pcap_dumper);
511 }
512
513 #define PCAP_SINK_OPEN(port, file_name, max_n_pkts)             \
514         pcap_sink_open(port, file_name, max_n_pkts)
515
516 #else
517
518 #define PCAP_SINK_OPEN(port, file_name, max_n_pkts)             \
519 ({                                                              \
520         int _ret = 0;                                           \
521                                                                 \
522         if (file_name) {                                        \
523                 RTE_LOG(ERR, PORT, "Sink port field "           \
524                         "\"file_name\" is not NULL.\n");        \
525                 _ret = -1;                                      \
526         }                                                       \
527                                                                 \
528         _ret;                                                   \
529 })
530
531 static void
532 pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port,
533                 __rte_unused struct rte_mbuf *mbuf) {}
534
535 static void
536 pcap_sink_flush_pkt(__rte_unused void *dumper) {}
537
538 static void
539 pcap_sink_close(__rte_unused void *dumper) {}
540
541 #endif
542
543 static void *
544 rte_port_sink_create(void *params, int socket_id)
545 {
546         struct rte_port_sink *port;
547         struct rte_port_sink_params *p = params;
548
549         /* Memory allocation */
550         port = rte_zmalloc_socket("PORT", sizeof(*port),
551                         RTE_CACHE_LINE_SIZE, socket_id);
552         if (port == NULL) {
553                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
554                 return NULL;
555         }
556
557         if (!p)
558                 return port;
559
560         if (p->file_name) {
561                 int status = PCAP_SINK_OPEN(port, p->file_name,
562                         p->max_n_pkts);
563
564                 if (status < 0) {
565                         rte_free(port);
566                         port = NULL;
567                 }
568         }
569
570         return port;
571 }
572
573 static int
574 rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
575 {
576         __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
577
578         RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
579         if (p->dumper != NULL)
580                 pcap_sink_dump_pkt(p, pkt);
581         rte_pktmbuf_free(pkt);
582         RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
583
584         return 0;
585 }
586
587 static int
588 rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
589         uint64_t pkts_mask)
590 {
591         __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
592
593         if ((pkts_mask & (pkts_mask + 1)) == 0) {
594                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
595                 uint32_t i;
596
597                 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
598                 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
599
600                 if (p->dumper) {
601                         for (i = 0; i < n_pkts; i++) {
602                                 struct rte_mbuf *pkt = pkts[i];
603
604                                 pcap_sink_dump_pkt(p, pkt);
605                         }
606                 }
607
608                 for (i = 0; i < n_pkts; i++) {
609                         struct rte_mbuf *pkt = pkts[i];
610
611                         rte_pktmbuf_free(pkt);
612                 }
613
614         } else {
615                 if (p->dumper) {
616                         uint64_t dump_pkts_mask = pkts_mask;
617                         uint32_t pkt_index;
618
619                         for ( ; dump_pkts_mask; ) {
620                                 pkt_index = __builtin_ctzll(
621                                         dump_pkts_mask);
622                                 pcap_sink_dump_pkt(p, pkts[pkt_index]);
623                                 dump_pkts_mask &= ~(1LLU << pkt_index);
624                         }
625                 }
626
627                 for ( ; pkts_mask; ) {
628                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
629                         uint64_t pkt_mask = 1LLU << pkt_index;
630                         struct rte_mbuf *pkt = pkts[pkt_index];
631
632                         RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
633                         RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
634                         rte_pktmbuf_free(pkt);
635                         pkts_mask &= ~pkt_mask;
636                 }
637         }
638
639         return 0;
640 }
641
642 static int
643 rte_port_sink_flush(void *port)
644 {
645         struct rte_port_sink *p = (struct rte_port_sink *)port;
646
647         if (p->dumper != NULL)
648                 pcap_sink_flush_pkt(p->dumper);
649
650         return 0;
651 }
652
653 static int
654 rte_port_sink_free(void *port)
655 {
656         struct rte_port_sink *p =
657                         (struct rte_port_sink *)port;
658         /* Check input parameters */
659         if (p == NULL)
660                 return 0;
661
662         if (p->dumper != NULL)
663                 pcap_sink_close(p->dumper);
664
665         rte_free(p);
666
667         return 0;
668 }
669
670 static int
671 rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
672                 int clear)
673 {
674         struct rte_port_sink *p =
675                 (struct rte_port_sink *) port;
676
677         if (stats != NULL)
678                 memcpy(stats, &p->stats, sizeof(p->stats));
679
680         if (clear)
681                 memset(&p->stats, 0, sizeof(p->stats));
682
683         return 0;
684 }
685
686 /*
687  * Summary of port operations
688  */
689 struct rte_port_in_ops rte_port_source_ops = {
690         .f_create = rte_port_source_create,
691         .f_free = rte_port_source_free,
692         .f_rx = rte_port_source_rx,
693         .f_stats = rte_port_source_stats_read,
694 };
695
696 struct rte_port_out_ops rte_port_sink_ops = {
697         .f_create = rte_port_sink_create,
698         .f_free = rte_port_sink_free,
699         .f_tx = rte_port_sink_tx,
700         .f_tx_bulk = rte_port_sink_tx_bulk,
701         .f_flush = rte_port_sink_flush,
702         .f_stats = rte_port_sink_stats_read,
703 };