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.
20 #include "mbuf_utils.h"
21 #include "task_init.h"
22 #include "task_base.h"
25 #include "prox_port_cfg.h"
28 /* Task that sends packets to multiple outputs. Note that in case of n
29 outputs, the output packet rate is n times the input packet
30 rate. Also, since the packet is duplicated by increasing the
31 refcnt, a change to a packet in subsequent tasks connected through
32 one of the outputs of this task will also change the packets as
33 seen by tasks connected behind through other outputs. The correct
34 way to resolve this is to create deep copies of the packet. */
36 struct task_base base;
40 struct task_mirror_copy {
41 struct task_base base;
42 struct rte_mempool *mempool;
46 static int handle_mirror_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
49 struct task_mirror *task = (struct task_mirror *)tbase;
50 uint8_t out[MAX_PKT_BURST];
51 struct rte_mbuf *mbufs2[MAX_PKT_BURST];
53 /* Since after calling tx_pkt the mbufs parameter of a handle
54 function becomes invalid and handle_mirror calls tx_pkt
55 multiple times, the pointers are copied first. This copy is
56 used in each call to tx_pkt below. */
57 rte_memcpy(mbufs2, mbufs, sizeof(mbufs[0]) * n_pkts);
59 for (uint16_t j = 0; j < n_pkts; ++j) {
60 rte_pktmbuf_refcnt_update(mbufs2[j], task->n_dests - 1);
62 for (uint16_t j = 0; j < task->n_dests; ++j) {
63 memset(out, j, n_pkts);
65 ret+= task->base.tx_pkt(&task->base, mbufs2, n_pkts, out);
70 static int handle_mirror_bulk_copy(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
72 struct task_mirror_copy *task = (struct task_mirror_copy *)tbase;
73 uint8_t out[MAX_PKT_BURST];
76 /* Send copies of the packet to all but the first
78 struct rte_mbuf *new_pkts[MAX_PKT_BURST];
80 for (uint16_t j = 1; j < task->n_dests; ++j) {
81 if (rte_mempool_get_bulk(task->mempool, (void **)new_pkts, n_pkts) < 0) {
84 /* Finally, forward the incoming packets. */
85 for (uint16_t i = 0; i < n_pkts; ++i) {
90 init_mbuf_seg(new_pkts[i]);
92 pkt_len = rte_pktmbuf_pkt_len(mbufs[i]);
93 rte_pktmbuf_pkt_len(new_pkts[i]) = pkt_len;
94 rte_pktmbuf_data_len(new_pkts[i]) = pkt_len;
96 dst = rte_pktmbuf_mtod(new_pkts[i], void *);
97 src = rte_pktmbuf_mtod(mbufs[i], void *);
99 rte_memcpy(dst, src, pkt_len);
101 ret+= task->base.tx_pkt(&task->base, new_pkts, n_pkts, out);
104 /* Finally, forward the incoming packets to the first destination. */
105 memset(out, 0, n_pkts);
106 ret+= task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
110 static void init_task_mirror(struct task_base *tbase, struct task_args *targ)
112 struct task_mirror *task = (struct task_mirror *)tbase;
114 task->n_dests = targ->nb_txports? targ->nb_txports : targ->nb_txrings;
117 static void init_task_mirror_copy(struct task_base *tbase, struct task_args *targ)
119 static char name[] = "mirror_pool";
120 struct task_mirror_copy *task = (struct task_mirror_copy *)tbase;
121 const int sock_id = rte_lcore_to_socket_id(targ->lconf->id);
122 task->n_dests = targ->nb_txports? targ->nb_txports : targ->nb_txrings;
125 task->mempool = rte_mempool_create(name,
126 targ->nb_mbuf - 1, MBUF_SIZE,
128 sizeof(struct rte_pktmbuf_pool_private),
129 rte_pktmbuf_pool_init, NULL,
132 PROX_PANIC(task->mempool == NULL,
133 "Failed to allocate memory pool on socket %u with %u elements\n",
134 sock_id, targ->nb_mbuf - 1);
135 task->n_dests = targ->nb_txports? targ->nb_txports : targ->nb_txrings;
138 static struct task_init task_init_mirror = {
139 .mode_str = "mirror",
140 .init = init_task_mirror,
141 .handle = handle_mirror_bulk,
142 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS | TASK_FEATURE_TXQ_FLAGS_REFCOUNT,
143 .size = sizeof(struct task_mirror),
146 static struct task_init task_init_mirror2 = {
147 .mode_str = "mirror",
148 .sub_mode_str = "copy",
149 .init = init_task_mirror_copy,
150 .handle = handle_mirror_bulk_copy,
151 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS,
152 .size = sizeof(struct task_mirror),
155 __attribute__((constructor)) static void reg_task_mirror(void)
157 reg_task(&task_init_mirror);
158 reg_task(&task_init_mirror2);