2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include <rte_cycles.h>
20 #include "prox_malloc.h"
24 #include "task_init.h"
25 #include "task_base.h"
27 #include "prox_compat.h"
30 struct task_base base;
32 struct rte_mbuf **mbufs;
37 static uint16_t buffer_packets(struct task_dump *task, struct rte_mbuf **mbufs, uint16_t n_pkts)
41 if (task->n_mbufs == task->n_pkts)
44 for (j = 0; j < n_pkts && task->n_mbufs < task->n_pkts; ++j) {
45 mbufs[j]->udata64 = rte_rdtsc();
46 task->mbufs[task->n_mbufs++] = mbufs[j];
52 static int handle_dump_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
54 struct task_dump *task = (struct task_dump *)tbase;
55 const uint16_t ofs = buffer_packets(task, mbufs, n_pkts);
57 for (uint16_t j = ofs; j < n_pkts; ++j)
58 rte_pktmbuf_free(mbufs[j]);
59 TASK_STATS_ADD_DROP_DISCARD(&tbase->aux->stats, n_pkts - ofs);
63 static void init_task_dump(struct task_base *tbase, __attribute__((unused)) struct task_args *targ)
65 struct task_dump *task = (struct task_dump *)tbase;
66 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
68 if (targ->n_pkts == 0)
69 targ->n_pkts = 64 * 1024;
70 task->mbufs = prox_zmalloc(sizeof(*task->mbufs) * targ->n_pkts, socket_id);
71 task->n_pkts = targ->n_pkts;
72 if (!strcmp(targ->pcap_file, "")) {
73 strcpy(targ->pcap_file, "out.pcap");
75 prox_strncpy(task->pcap_file, targ->pcap_file, sizeof(task->pcap_file));
78 static void stop(struct task_base *tbase)
80 struct task_dump *task = (struct task_dump *)tbase;
81 static pcap_dumper_t *pcap_dump_handle;
83 uint32_t n_pkts = 65536;
84 struct pcap_pkthdr header = {{0}, 0, 0};
86 char err_str[PCAP_ERRBUF_SIZE];
87 const uint64_t hz = rte_get_tsc_hz();
88 struct timeval tv = {0};
89 uint64_t tsc, beg = 0;
91 plogx_info("Dumping %d packets to '%s'\n", task->n_mbufs, task->pcap_file);
92 handle = pcap_open_dead(DLT_EN10MB, n_pkts);
93 pcap_dump_handle = pcap_dump_open(handle, task->pcap_file);
96 beg = task->mbufs[0]->udata64;
98 for (uint32_t j = 0; j < task->n_mbufs; ++j) {
99 tsc = task->mbufs[j]->udata64 - beg;
100 header.len = rte_pktmbuf_pkt_len(task->mbufs[j]);
101 header.caplen = header.len;
102 tsc_to_tv(&header.ts, tsc);
103 pcap_dump((unsigned char *)pcap_dump_handle, &header, rte_pktmbuf_mtod(task->mbufs[j], void *));
106 pcap_dump_close(pcap_dump_handle);
108 plogx_info("Dump complete, releasing mbufs\n");
112 while (j + 64 < task->n_mbufs) {
113 tbase->tx_pkt(tbase, &task->mbufs[j], 64, NULL);
116 if (j < task->n_mbufs) {
117 tbase->tx_pkt(tbase, &task->mbufs[j], task->n_mbufs - j, NULL);
122 static struct task_init task_init_dump = {
124 .init = init_task_dump,
125 .handle = handle_dump_bulk,
127 .flag_features = TASK_FEATURE_ZERO_RX,
128 .size = sizeof(struct task_dump)
131 __attribute__((constructor)) static void reg_task_dump(void)
133 reg_task(&task_init_dump);