Increase default mbuf size and code simplification/cleanup
[samplevnf.git] / VNFs / DPPD-PROX / handle_l2fwd.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 <rte_mbuf.h>
18
19 #include "task_init.h"
20 #include "task_base.h"
21 #include "lconf.h"
22 #include "log.h"
23 #include "prox_port_cfg.h"
24
25 struct task_l2fwd {
26         struct task_base base;
27         uint8_t src_dst_mac[12];
28         uint32_t runtime_flags;
29 };
30
31 static int handle_l2fwd_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
32 {
33         struct task_l2fwd *task = (struct task_l2fwd *)tbase;
34         struct ether_hdr *hdr;
35         struct ether_addr mac;
36
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], struct ether_hdr *);
41                         rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
42                 }
43         } else {
44                 for (uint16_t j = 0; j < n_pkts; ++j) {
45                         hdr = rte_pktmbuf_mtod(mbufs[j], struct 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                                 ether_addr_copy(&hdr->d_addr, &mac);
49                         }
50
51                         if (task->runtime_flags & TASK_ARG_DST_MAC_SET)
52                                 ether_addr_copy((struct 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                                 ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
55
56                         if (task->runtime_flags & TASK_ARG_SRC_MAC_SET) {
57                                 ether_addr_copy((struct 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                                 ether_addr_copy(&mac, &hdr->s_addr);
60                         }
61                 }
62         }
63         return task->base.tx_pkt(&task->base, mbufs, n_pkts, NULL);
64 }
65
66 static void init_task_l2fwd(struct task_base *tbase, struct task_args *targ)
67 {
68         struct task_l2fwd *task = (struct task_l2fwd *)tbase;
69         struct ether_addr *src_addr, *dst_addr;
70
71         /*
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
77          *
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
86                                                     is used as src mac
87          */
88
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));
92         }
93
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));
106                 }
107         }
108         task->runtime_flags = targ->flags;
109 }
110
111 static struct task_init task_init_l2fwd = {
112         .mode_str = "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),
117 };
118
119 __attribute__((constructor)) static void reg_task_l2fwd(void)
120 {
121         reg_task(&task_init_l2fwd);
122 }