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.
19 #include "task_init.h"
20 #include "task_base.h"
23 #include "prox_port_cfg.h"
26 struct task_base base;
27 uint8_t src_dst_mac[12];
28 uint32_t runtime_flags;
31 static int handle_l2fwd_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
33 struct task_l2fwd *task = (struct task_l2fwd *)tbase;
34 prox_rte_ether_hdr *hdr;
35 prox_rte_ether_addr mac;
37 if ((task->runtime_flags & (TASK_ARG_DST_MAC_SET|TASK_ARG_SRC_MAC_SET)) == (TASK_ARG_DST_MAC_SET|TASK_ARG_SRC_MAC_SET)) {
38 /* Source and Destination mac hardcoded */
39 for (uint16_t j = 0; j < n_pkts; ++j) {
40 hdr = rte_pktmbuf_mtod(mbufs[j], prox_rte_ether_hdr *);
41 rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
44 for (uint16_t j = 0; j < n_pkts; ++j) {
45 hdr = rte_pktmbuf_mtod(mbufs[j], prox_rte_ether_hdr *);
46 if ((task->runtime_flags & (TASK_ARG_DO_NOT_SET_SRC_MAC|TASK_ARG_SRC_MAC_SET)) == 0) {
47 /* dst mac will be used as src mac */
48 prox_rte_ether_addr_copy(&hdr->d_addr, &mac);
51 if (task->runtime_flags & TASK_ARG_DST_MAC_SET)
52 prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[0], &hdr->d_addr);
53 else if ((task->runtime_flags & TASK_ARG_DO_NOT_SET_DST_MAC) == 0)
54 prox_rte_ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
56 if (task->runtime_flags & TASK_ARG_SRC_MAC_SET) {
57 prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
58 } else if ((task->runtime_flags & TASK_ARG_DO_NOT_SET_SRC_MAC) == 0) {
59 prox_rte_ether_addr_copy(&mac, &hdr->s_addr);
63 return task->base.tx_pkt(&task->base, mbufs, n_pkts, NULL);
66 static void init_task_l2fwd(struct task_base *tbase, struct task_args *targ)
68 struct task_l2fwd *task = (struct task_l2fwd *)tbase;
69 prox_rte_ether_addr *src_addr, *dst_addr;
72 * The destination MAC of the outgoing packet is based on the config file:
73 * - 'dst mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as dst mac
74 * - 'dst mac=packet' => the src mac of the incoming packet is used as dst mac
75 * - 'dst mac=no' => the dst mac is untouched
76 * - (default - no 'dst mac') => the src mac from the incoming packet is used as dst mac
78 * The source MAC of the outgoing packet is based on the config file:
79 * - 'src mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as src mac
80 * - 'src mac=packet' => the dst mac of the incoming packet is used as src mac
81 * - 'src mac=hw' => the mac address of the tx port is used as src mac
82 * An error is returned if there are no physical tx ports
83 * - 'src mac=no' => the src mac is untouched
84 * - (default - no 'src mac') => if there is physical tx port, the mac of that port is used as src mac
85 * - (default - no 'src mac') if there are no physical tx ports the dst mac of the incoming packet
89 if (targ->flags & TASK_ARG_DST_MAC_SET) {
90 dst_addr = &targ->edaddr;
91 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
94 if (targ->flags & TASK_ARG_SRC_MAC_SET) {
95 src_addr = &targ->esaddr;
96 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
97 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
98 } else if ((targ->flags & TASK_ARG_DO_NOT_SET_SRC_MAC) == 0) {
99 if (targ->flags & TASK_ARG_HW_SRC_MAC)
100 PROX_PANIC(targ->nb_txports == 0, "src mac set to hw but no tx port\n");
101 if (targ->nb_txports) {
102 src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
103 targ->flags |= TASK_ARG_SRC_MAC_SET;
104 plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
105 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
108 task->runtime_flags = targ->flags;
111 static struct task_init task_init_l2fwd = {
113 .init = init_task_l2fwd,
114 .handle = handle_l2fwd_bulk,
115 .flag_features = TASK_FEATURE_NEVER_DISCARDS|TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS,
116 .size = sizeof(struct task_l2fwd),
119 __attribute__((constructor)) static void reg_task_l2fwd(void)
121 reg_task(&task_init_l2fwd);