1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(C) 2023 Marvell.
*/
#include <eal_export.h>
#include <rte_ethdev.h>
#include <rte_ether.h>
#include <rte_graph.h>
#include <rte_graph_worker.h>
#include <rte_ip.h>
#include <rte_malloc.h>
#include <rte_vect.h>
#include "rte_node_ip6_api.h"
#include "ip6_rewrite_priv.h"
#include "node_private.h"
struct ip6_rewrite_node_ctx {
/* Dynamic offset to mbuf priv1 */
int mbuf_priv1_off;
/* Cached next index */
uint16_t next_index;
};
static struct ip6_rewrite_node_main *ip6_rewrite_nm;
#define IP6_REWRITE_NODE_LAST_NEXT(ctx) \
(((struct ip6_rewrite_node_ctx *)ctx)->next_index)
#define IP6_REWRITE_NODE_PRIV1_OFF(ctx) \
(((struct ip6_rewrite_node_ctx *)ctx)->mbuf_priv1_off)
static uint16_t
ip6_rewrite_node_process(struct rte_graph *graph, struct rte_node *node,
void **objs, uint16_t nb_objs)
{
struct rte_mbuf *mbuf0, *mbuf1, *mbuf2, *mbuf3, **pkts;
struct ip6_rewrite_nh_header *nh = ip6_rewrite_nm->nh;
const int dyn = IP6_REWRITE_NODE_PRIV1_OFF(node->ctx);
uint16_t next0, next1, next2, next3, next_index;
uint16_t n_left_from, held = 0, last_spec = 0;
struct rte_ipv6_hdr *ip0, *ip1, *ip2, *ip3;
void *d0, *d1, *d2, *d3;
void **to_next, **from;
rte_xmm_t priv01;
rte_xmm_t priv23;
int i;
/* Speculative next as last next */
next_index = IP6_REWRITE_NODE_LAST_NEXT(node->ctx);
rte_prefetch0(nh);
pkts = (struct rte_mbuf **)objs;
from = objs;
n_left_from = nb_objs;
for (i = 0; i < 4 && i < n_left_from; i++)
rte_prefetch0(pkts[i]);
/* Get stream for the speculated next node */
to_next = rte_node_next_stream_get(graph, node, next_index, nb_objs);
/* Update Ethernet header of pkts */
while (n_left_from >= 4) {
if (likely(n_left_from > 7)) {
/* Prefetch only next-mbuf struct and priv area.
* Data need not be prefetched as we only write.
*/
rte_prefetch0(pkts[4]);
rte_prefetch0(pkts[5]);
rte_prefetch0(pkts[6]);
rte_prefetch0(pkts[7]);
}
mbuf0 = pkts[0];
mbuf1 = pkts[1];
mbuf2 = pkts[2];
mbuf3 = pkts[3];
pkts += 4;
n_left_from -= 4;
priv01.u64[0] = node_mbuf_priv1(mbuf0, dyn)->u;
priv01.u64[1] = node_mbuf_priv1(mbuf1, dyn)->u;
priv23.u64[0] = node_mbuf_priv1(mbuf2, dyn)->u;
priv23.u64[1] = node_mbuf_priv1(mbuf3, dyn)->u;
/* Update next_hop rewrite ethernet hdr on mbuf0 */
d0 = rte_pktmbuf_mtod(mbuf0, void *);
rte_memcpy(d0, nh[priv01.u16[0]].rewrite_data,
nh[priv01.u16[0]].rewrite_len);
next0 = nh[priv01.u16[0]].tx_node;
ip0 = (struct rte_ipv6_hdr *)((uint8_t *)d0 +
sizeof(struct rte_ether_hdr));
ip0->hop_limits = priv01.u16[1] - 1;
/* Update next_hop rewrite ethernet hdr on mbuf1 */
d1 = rte_pktmbuf_mtod(mbuf1, void *);
rte_memcpy(d1, nh[priv01.u16[4]].rewrite_data,
nh[priv01.u16[4]].rewrite_len);
next1 = nh[priv01.u16[4]].tx_node;
ip1 = (struct rte_ipv6_hdr *)((uint8_t *)d1 +
sizeof(struct rte_ether_hdr));
ip1->hop_limits = priv01.u16[5] - 1;
/* Update next_hop rewrite ethernet hdr on mbuf2 */
d2 = rte_pktmbuf_mtod(mbuf2, void *);
rte_memcpy(d2, nh[priv23.u16[0]].rewrite_data,
nh[priv23.u16[0]].rewrite_len);
next2 = nh[priv23.u16[0]].tx_node;
ip2 = (struct rte_ipv6_hdr *)((uint8_t *)d2 +
sizeof(struct rte_ether_hdr));
ip2->hop_limits = priv23.u16[1] - 1;
/* Update next_hop rewrite ethernet hdr on mbuf3 */
d3 = rte_pktmbuf_mtod(mbuf3, void *);
rte_memcpy(d3, nh[priv23.u16[4]].rewrite_data,
nh[priv23.u16[4]].rewrite_len);
next3 = nh[priv23.u16[4]].tx_node;
ip3 = (struct rte_ipv6_hdr *)((uint8_t *)d3 +
sizeof(struct rte_ether_hdr));
ip3->hop_limits = priv23.u16[5] - 1;
/* Enqueue four packets to next node */
rte_edge_t fix_spec =
((next_index == next0) && (next0 == next1) &&
(next1 == next2) && (next2 == next3));
if (unlikely(fix_spec == 0)) {
/* Copy things successfully speculated till now */
rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
from += last_spec;
to_next += last_spec;
held += last_spec;
last_spec = 0;
/* next0 */
if (next_index == next0) {
to_next[0] = from[0];
to_next++;
held++;
} else {
rte_node_enqueue_x1(graph, node, next0,
from[0]);
}
/* next1 */
if (next_index == next1) {
to_next[0] = from[1];
to_next++;
held++;
} else {
rte_node_enqueue_x1(graph, node, next1,
from[1]);
}
/* next2 */
if (next_index == next2) {
to_next[0] = from[2];
to_next++;
held++;
} else {
rte_node_enqueue_x1(graph, node, next2,
from[2]);
}
/* next3 */
if (next_index == next3) {
to_next[0] = from[3];
to_next++;
held++;
} else {
rte_node_enqueue_x1(graph, node, next3,
from[3]);
}
from += 4;
/* Change speculation if last two are same */
if ((next_index != next3) && (next2 == next3)) {
/* Put the current speculated node */
rte_node_next_stream_put(graph, node,
next_index, held);
held = 0;
/* Get next speculated stream */
next_index = next3;
to_next = rte_node_next_stream_get(
graph, node, next_index, nb_objs);
}
} else {
last_spec += 4;
}
}
while (n_left_from > 0) {
mbuf0 = pkts[0];
pkts += 1;
n_left_from -= 1;
d0 = rte_pktmbuf_mtod(mbuf0, void *);
rte_memcpy(d0, nh[node_mbuf_priv1(mbuf0, dyn)->nh].rewrite_data,
nh[node_mbuf_priv1(mbuf0, dyn)->nh].rewrite_len);
next0 = nh[node_mbuf_priv1(mbuf0, dyn)->nh].tx_node;
ip0 = (struct rte_ipv6_hdr *)((uint8_t *)d0 +
sizeof(struct rte_ether_hdr));
ip0->hop_limits = node_mbuf_priv1(mbuf0, dyn)->ttl - 1;
if (unlikely(next_index ^ next0)) {
/* Copy things successfully speculated till now */
rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
from += last_spec;
to_next += last_spec;
held += last_spec;
last_spec = 0;
rte_node_enqueue_x1(graph, node, next0, from[0]);
from += 1;
} else {
last_spec += 1;
}
}
/* !!! Home run !!! */
if (likely(last_spec == nb_objs)) {
rte_node_next_stream_move(graph, node, next_index);
return nb_objs;
}
held += last_spec;
rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
rte_node_next_stream_put(graph, node, next_index, held);
/* Save the last next used */
IP6_REWRITE_NODE_LAST_NEXT(node->ctx) = next_index;
return nb_objs;
}
static int
ip6_rewrite_node_init(const struct rte_graph *graph, struct rte_node *node)
{
int dyn;
RTE_SET_USED(graph);
RTE_BUILD_BUG_ON(sizeof(struct ip6_rewrite_node_ctx) > RTE_NODE_CTX_SZ);
dyn = rte_node_mbuf_dynfield_register();
if (dyn < 0)
return -rte_errno;
IP6_REWRITE_NODE_PRIV1_OFF(node->ctx) = dyn;
node_dbg("ip6_rewrite", "Initialized ip6_rewrite node");
return 0;
}
int
ip6_rewrite_set_next(uint16_t port_id, uint16_t next_index)
{
if (ip6_rewrite_nm == NULL) {
ip6_rewrite_nm = rte_zmalloc(
"ip6_rewrite", sizeof(struct ip6_rewrite_node_main),
RTE_CACHE_LINE_SIZE);
if (ip6_rewrite_nm == NULL)
return -ENOMEM;
}
ip6_rewrite_nm->next_index[port_id] = next_index;
return 0;
}
RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_node_ip6_rewrite_add, 23.07)
int
rte_node_ip6_rewrite_add(uint16_t next_hop, uint8_t *rewrite_data,
uint8_t rewrite_len, uint16_t dst_port)
{
struct ip6_rewrite_nh_header *nh;
if (next_hop >= RTE_GRAPH_IP6_REWRITE_MAX_NH)
return -EINVAL;
if (rewrite_len > RTE_GRAPH_IP6_REWRITE_MAX_LEN)
return -EINVAL;
if (ip6_rewrite_nm == NULL) {
ip6_rewrite_nm = rte_zmalloc(
"ip6_rewrite", sizeof(struct ip6_rewrite_node_main),
RTE_CACHE_LINE_SIZE);
if (ip6_rewrite_nm == NULL)
return -ENOMEM;
}
/* Check if dst port doesn't exist as edge */
if (!ip6_rewrite_nm->next_index[dst_port])
return -EINVAL;
/* Update next hop */
nh = &ip6_rewrite_nm->nh[next_hop];
memcpy(nh->rewrite_data, rewrite_data, rewrite_len);
nh->tx_node = ip6_rewrite_nm->next_index[dst_port];
nh->rewrite_len = rewrite_len;
nh->enabled = true;
return 0;
}
static struct rte_node_register ip6_rewrite_node = {
.process = ip6_rewrite_node_process,
.name = "ip6_rewrite",
/* Default edge i.e '0' is pkt drop */
.nb_edges = 1,
.next_nodes = {
[0] = "pkt_drop",
},
.init = ip6_rewrite_node_init,
};
struct rte_node_register *
ip6_rewrite_node_get(void)
{
return &ip6_rewrite_node;
}
RTE_NODE_REGISTER(ip6_rewrite_node);
|