Support DPDK 19.11 rc0
[samplevnf.git] / VNFs / DPPD-PROX / handle_mirror.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #include <string.h>
18 #include <rte_mbuf.h>
19
20 #include "mbuf_utils.h"
21 #include "task_init.h"
22 #include "task_base.h"
23 #include "lconf.h"
24 #include "log.h"
25 #include "prox_port_cfg.h"
26 #include "quit.h"
27
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. */
35 struct task_mirror {
36         struct task_base base;
37         uint32_t         n_dests;
38 };
39
40 struct task_mirror_copy {
41         struct task_base   base;
42         struct rte_mempool *mempool;
43         uint32_t           n_dests;
44 };
45
46 static int handle_mirror_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
47 {
48         int ret = 0;
49         struct task_mirror *task = (struct task_mirror *)tbase;
50         uint8_t out[MAX_PKT_BURST];
51         struct rte_mbuf *mbufs2[MAX_PKT_BURST];
52
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);
58
59         for (uint16_t j = 0; j < n_pkts; ++j) {
60                 rte_pktmbuf_refcnt_update(mbufs2[j], task->n_dests - 1);
61         }
62         for (uint16_t j = 0; j < task->n_dests; ++j) {
63                 memset(out, j, n_pkts);
64
65                 ret+= task->base.tx_pkt(&task->base, mbufs2, n_pkts, out);
66         }
67         return ret;
68 }
69
70 static int handle_mirror_bulk_copy(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
71 {
72         struct task_mirror_copy *task = (struct task_mirror_copy *)tbase;
73         uint8_t out[MAX_PKT_BURST];
74         int ret = 0;
75
76         /* Send copies of the packet to all but the first
77            destination */
78         struct rte_mbuf *new_pkts[MAX_PKT_BURST];
79
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) {
82                         continue;
83                 }
84                 /* Finally, forward the incoming packets. */
85                 for (uint16_t i = 0; i < n_pkts; ++i) {
86                         void *dst, *src;
87                         uint16_t pkt_len;
88
89                         out[i] = j;
90                         init_mbuf_seg(new_pkts[i]);
91
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;
95
96                         dst = rte_pktmbuf_mtod(new_pkts[i], void *);
97                         src = rte_pktmbuf_mtod(mbufs[i], void *);
98
99                         rte_memcpy(dst, src, pkt_len);
100                 }
101                 ret+= task->base.tx_pkt(&task->base, new_pkts, n_pkts, out);
102         }
103
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);
107         return ret;
108 }
109
110 static void init_task_mirror(struct task_base *tbase, struct task_args *targ)
111 {
112         struct task_mirror *task = (struct task_mirror *)tbase;
113
114         task->n_dests = targ->nb_txports? targ->nb_txports : targ->nb_txrings;
115 }
116
117 static void init_task_mirror_copy(struct task_base *tbase, struct task_args *targ)
118 {
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;
123
124         name[0]++;
125         task->mempool = rte_mempool_create(name,
126                                            targ->nb_mbuf - 1, MBUF_SIZE,
127                                            targ->nb_cache_mbuf,
128                                            sizeof(struct rte_pktmbuf_pool_private),
129                                            rte_pktmbuf_pool_init, NULL,
130                                            rte_pktmbuf_init, 0,
131                                            sock_id, 0);
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;
136 }
137
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),
144 };
145
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),
153 };
154
155 __attribute__((constructor)) static void reg_task_mirror(void)
156 {
157         reg_task(&task_init_mirror);
158         reg_task(&task_init_mirror2);
159 }