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