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 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);
54 static int handle_arp_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
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;
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;
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;
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;
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;
87 out[n_other_pkts] = OUT_DISCARD;
88 mbufs[n_other_pkts] = mbufs[j];
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));
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;
97 out[n_other_pkts] = task->other_pkts_ring;
98 mbufs[n_other_pkts] = mbufs[j];
102 out[n_other_pkts] = task->other_pkts_ring;
103 mbufs[n_other_pkts] = mbufs[j];
109 if (n_arp_reply_pkts) {
110 ret+=task->base.aux->tx_pkt_hw(&task->base, replies_mbufs, n_arp_reply_pkts, out);
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);
119 void task_arp_set_local_ip(struct task_base *tbase, uint32_t ip)
121 struct task_arp *task = (struct task_arp *)tbase;
125 static void init_task_arp(struct task_base *tbase, struct task_args *targ)
127 struct task_arp *task = (struct task_arp *)tbase;
128 struct task_args *dtarg;
131 task->other_pkts_ring = OUT_DISCARD;
132 task->arp_replies_ring = OUT_DISCARD;
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));
137 task->ip = rte_cpu_to_be_32(targ->local_ipv4);
138 task->tmp_ip = task->ip;
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;
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;
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");
160 // Reply to ARP requests with random MAC addresses
161 static struct task_init task_init_cpe_arp = {
163 .init = init_task_arp,
164 .handle = handle_arp_bulk,
165 .size = sizeof(struct task_arp)
168 // Reply to ARP requests with MAC address of the interface
169 static struct task_init task_init_arp = {
171 .sub_mode_str = "local",
172 .init = init_task_arp,
173 .handle = handle_arp_bulk,
174 .size = sizeof(struct task_arp)
177 __attribute__((constructor)) static void reg_task_arp(void)
179 reg_task(&task_init_cpe_arp);
180 reg_task(&task_init_arp);