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.
17 #include "task_init.h"
18 #include "task_base.h"
24 #include "prox_port_cfg.h"
26 #include "cmd_parser.h"
27 #include "handle_arp.h"
30 struct task_base base;
31 struct ether_addr src_mac;
36 uint8_t arp_replies_ring;
37 uint8_t other_pkts_ring;
38 uint8_t send_arp_requests;
41 static void task_update_config(struct task_arp *task)
43 if (unlikely(task->ip != task->tmp_ip))
44 task->ip = task->tmp_ip;
47 static void handle_arp(struct task_arp *task, struct ether_hdr_arp *hdr, struct ether_addr *s_addr)
49 build_arp_reply(hdr, s_addr);
52 static int handle_arp_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
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;
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;
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;
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;
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;
85 out[n_other_pkts] = OUT_DISCARD;
86 mbufs[n_other_pkts] = mbufs[j];
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));
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;
95 out[n_other_pkts] = task->other_pkts_ring;
96 mbufs[n_other_pkts] = mbufs[j];
100 out[n_other_pkts] = task->other_pkts_ring;
101 mbufs[n_other_pkts] = mbufs[j];
107 if (n_arp_reply_pkts) {
108 ret+=task->base.aux->tx_pkt_hw(&task->base, replies_mbufs, n_arp_reply_pkts, out);
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);
117 void task_arp_set_local_ip(struct task_base *tbase, uint32_t ip)
119 struct task_arp *task = (struct task_arp *)tbase;
123 static void init_task_arp(struct task_base *tbase, struct task_args *targ)
125 struct task_arp *task = (struct task_arp *)tbase;
126 struct task_args *dtarg;
129 task->other_pkts_ring = OUT_DISCARD;
130 task->arp_replies_ring = OUT_DISCARD;
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));
135 task->ip = rte_cpu_to_be_32(targ->local_ipv4);
136 task->tmp_ip = task->ip;
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;
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;
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");
158 // Reply to ARP requests with random MAC addresses
159 static struct task_init task_init_cpe_arp = {
161 .init = init_task_arp,
162 .handle = handle_arp_bulk,
163 .size = sizeof(struct task_arp)
166 // Reply to ARP requests with MAC address of the interface
167 static struct task_init task_init_arp = {
169 .sub_mode_str = "local",
170 .init = init_task_arp,
171 .handle = handle_arp_bulk,
172 .size = sizeof(struct task_arp)
175 __attribute__((constructor)) static void reg_task_arp(void)
177 reg_task(&task_init_cpe_arp);
178 reg_task(&task_init_arp);