Merge changes from PROX-v041
[samplevnf.git] / VNFs / DPPD-PROX / handle_arp.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 "task_init.h"
18 #include "task_base.h"
19 #include "stats.h"
20 #include "arp.h"
21 #include "etypes.h"
22 #include "quit.h"
23 #include "log.h"
24 #include "prox_port_cfg.h"
25 #include "lconf.h"
26 #include "cmd_parser.h"
27 #include "handle_arp.h"
28
29 struct task_arp {
30         struct task_base   base;
31         struct ether_addr  src_mac;
32         uint32_t           seed;
33         uint32_t           flags;
34         uint32_t           ip;
35         uint32_t           tmp_ip;
36         uint8_t            arp_replies_ring;
37         uint8_t            other_pkts_ring;
38         uint8_t            send_arp_requests;
39 };
40
41 static void task_update_config(struct task_arp *task)
42 {
43         if (unlikely(task->ip != task->tmp_ip))
44                 task->ip = task->tmp_ip;
45 }
46
47 static void handle_arp(struct task_arp *task, struct ether_hdr_arp *hdr, struct ether_addr *s_addr)
48 {
49         build_arp_reply(hdr, s_addr);
50 }
51
52 static int handle_arp_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
53 {
54         struct ether_hdr_arp *hdr;
55         struct task_arp *task = (struct task_arp *)tbase;
56         uint8_t out[MAX_PKT_BURST] = {0};
57         struct rte_mbuf *replies_mbufs[64] = {0}, *arp_pkt_mbufs[64] = {0};
58         int n_arp_reply_pkts = 0, n_other_pkts = 0,n_arp_pkts = 0;
59         struct ether_addr s_addr;
60
61         for (uint16_t j = 0; j < n_pkts; ++j) {
62                 hdr = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr_arp *);
63                 if (hdr->ether_hdr.ether_type == ETYPE_ARP) {
64                         if (arp_is_gratuitous(hdr)) {
65                                 out[n_other_pkts] = OUT_DISCARD;
66                                 n_other_pkts++;
67                                 plog_info("Received gratuitous packet \n");
68                         } else if (hdr->arp.oper == 0x100) {
69                                 if (task->arp_replies_ring != OUT_DISCARD) {
70                                         arp_pkt_mbufs[n_arp_pkts] = mbufs[j];
71                                         out[n_arp_pkts] = task->arp_replies_ring;
72                                         n_arp_pkts++;
73                                 } else if (task->ip == 0) {
74                                         create_mac(hdr, &s_addr);
75                                         handle_arp(task, hdr, &s_addr);
76                                         replies_mbufs[n_arp_reply_pkts] = mbufs[j];
77                                         out[n_arp_reply_pkts] = 0;
78                                         n_arp_reply_pkts++;
79                                 } else if (hdr->arp.data.tpa == task->ip) {
80                                         handle_arp(task, hdr, &task->src_mac);
81                                         replies_mbufs[n_arp_reply_pkts] = mbufs[j];
82                                         out[n_arp_reply_pkts] = 0;
83                                         n_arp_reply_pkts++;
84                                 } else {
85                                         out[n_other_pkts] = OUT_DISCARD;
86                                         mbufs[n_other_pkts] = mbufs[j];
87                                         n_other_pkts++;
88                                         plogx_dbg("Received ARP on unexpected IP %x, expecting %x\n", rte_be_to_cpu_32(hdr->arp.data.tpa), rte_be_to_cpu_32(task->ip));
89                                 }
90                         } else if (hdr->arp.oper == 0x200) {
91                                 arp_pkt_mbufs[n_arp_pkts] = mbufs[j];
92                                 out[n_arp_pkts] = task->arp_replies_ring;
93                                 n_arp_pkts++;
94                         } else {
95                                 out[n_other_pkts] = task->other_pkts_ring;
96                                 mbufs[n_other_pkts] = mbufs[j];
97                                 n_other_pkts++;
98                         }
99                 } else {
100                         out[n_other_pkts] = task->other_pkts_ring;
101                         mbufs[n_other_pkts] = mbufs[j];
102                         n_other_pkts++;
103                 }
104         }
105         int ret = 0;
106
107         if (n_arp_reply_pkts) {
108                 ret+=task->base.aux->tx_pkt_hw(&task->base, replies_mbufs, n_arp_reply_pkts, out);
109         }
110         if (n_arp_pkts)
111                 ret+= task->base.tx_pkt(&task->base, arp_pkt_mbufs, n_arp_pkts, out);
112         ret+= task->base.tx_pkt(&task->base, mbufs, n_other_pkts, out);
113         task_update_config(task);
114         return ret;
115 }
116
117 void task_arp_set_local_ip(struct task_base *tbase, uint32_t ip)
118 {
119         struct task_arp *task = (struct task_arp *)tbase;
120         task->tmp_ip = ip;
121 }
122
123 static void init_task_arp(struct task_base *tbase, struct task_args *targ)
124 {
125         struct task_arp *task = (struct task_arp *)tbase;
126         struct task_args *dtarg;
127         struct core_task ct;
128         int port_found = 0;
129         task->other_pkts_ring = OUT_DISCARD;
130         task->arp_replies_ring = OUT_DISCARD;
131
132         task->seed = rte_rdtsc();
133         memcpy(&task->src_mac, &prox_port_cfg[task->base.tx_params_hw_sw.tx_port_queue.port].eth_addr, sizeof(struct ether_addr));
134
135         task->ip = rte_cpu_to_be_32(targ->local_ipv4);
136         task->tmp_ip = task->ip;
137
138         PROX_PANIC(targ->nb_txrings > targ->core_task_set[0].n_elems, "%d txrings but %d elems in task_set\n", targ->nb_txrings, targ->core_task_set[0].n_elems);
139         for (uint32_t i = 0; i < targ->nb_txrings; ++i) {
140                 ct = targ->core_task_set[0].core_task[i];
141                 plog_info("ARP mode checking whether core %d task %d (i.e. ring %d) can handle arp\n", ct.core, ct.task, i);
142                 dtarg = core_targ_get(ct.core, ct.task);
143                 dtarg = find_reachable_task_sending_to_port(dtarg);
144                 if ((dtarg != NULL) && (task_is_sub_mode(dtarg->lconf->id, dtarg->id, "l3"))) {
145                         plog_info("ARP task sending ARP replies to core %d and task %d to handle them\n", ct.core, ct.task);
146                         task->arp_replies_ring = i;
147                 } else {
148                         plog_info("ARP task sending (potentially other) packets to core %d and task %d\n", ct.core, ct.task);
149                         task->other_pkts_ring = i;
150                 }
151         }
152
153         if ((targ->nb_txports == 0) && (task->arp_replies_ring == OUT_DISCARD)) {
154                 PROX_PANIC(1, "arp mode must have a tx_port or a ring able to a task in l3 reaching tx port");
155         }
156 }
157
158 // Reply to ARP requests with random MAC addresses
159 static struct task_init task_init_cpe_arp = {
160         .mode_str = "arp",
161         .init = init_task_arp,
162         .handle = handle_arp_bulk,
163         .size = sizeof(struct task_arp)
164 };
165
166 // Reply to ARP requests with MAC address of the interface
167 static struct task_init task_init_arp = {
168         .mode_str = "arp",
169         .sub_mode_str = "local",
170         .init = init_task_arp,
171         .handle = handle_arp_bulk,
172         .size = sizeof(struct task_arp)
173 };
174
175 __attribute__((constructor)) static void reg_task_arp(void)
176 {
177         reg_task(&task_init_cpe_arp);
178         reg_task(&task_init_arp);
179 }