79a5f02e33aada9878b289addcf0d81d39047341
[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          * Destination MAC can come from
73          *    - pre-configured mac in case 'dst mac=xx:xx:xx:xx:xx:xx' in config file
74          *    - src mac from the packet in case 'dst mac=packet' in config file
75          *    - not written in case 'dst mac=no' in config file
76          *    - (default - no 'dst mac') src mac from the packet
77          * Source MAC can come from
78          *    - pre-configured mac in case 'src mac=xx:xx:xx:xx:xx:xx' in config file
79          *    - dst mac from the packet in case 'src mac=packet' in config file
80          *    - not written in case 'src mac=no' in config file
81          *    - (default - no 'src mac') if (tx_port) port mac
82          *    - (default - no 'src mac') if (no tx_port) dst mac from the packet
83          */
84
85         if (targ->flags & TASK_ARG_DST_MAC_SET) {
86                 dst_addr = &targ->edaddr;
87                 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
88         }
89
90         if (targ->flags & TASK_ARG_SRC_MAC_SET) {
91                 src_addr =  &targ->esaddr;
92                 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
93                 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
94         } else if ((targ->flags & TASK_ARG_DO_NOT_SET_SRC_MAC) == 0) {
95                 if (targ->nb_txports) {
96                         src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
97                         targ->flags |= TASK_ARG_SRC_MAC_SET;
98                         plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
99                         memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
100                 }
101         }
102         task->runtime_flags = targ->flags;
103 }
104
105 static struct task_init task_init_l2fwd = {
106         .mode_str = "l2fwd",
107         .init = init_task_l2fwd,
108         .handle = handle_l2fwd_bulk,
109         .flag_features = TASK_FEATURE_NEVER_DISCARDS|TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
110         .size = sizeof(struct task_l2fwd),
111         .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
112 };
113
114 __attribute__((constructor)) static void reg_task_l2fwd(void)
115 {
116         reg_task(&task_init_l2fwd);
117 }