26#include <unordered_map>
28#include <seastar/net/net.hh>
29#include <seastar/core/byteorder.hh>
30#include <seastar/net/ethernet.hh>
31#include <seastar/util/modules.hh>
38class arp_for_protocol;
51 std::ignore = out_hash_data;
61 std::unordered_map<uint16_t, arp_for_protocol*> _arp_for_protocol;
68 static arp_hdr read(
const char* p) {
70 ah.htype = consume_be<uint16_t>(p);
71 ah.ptype = consume_be<uint16_t>(p);
74 static constexpr size_t size() {
return 4; }
79 void del(uint16_t proto_num);
84 std::optional<l3_protocol::l3packet> get_packet();
85 template <
class l3_proto>
93 using l3addr =
typename L3::address_type;
95 static constexpr auto max_waiters = 512;
111 static arp_hdr read(
const char* p) {
113 ah.htype = consume_be<uint16_t>(p);
114 ah.ptype = consume_be<uint16_t>(p);
115 ah.hlen = consume_be<uint8_t>(p);
116 ah.plen = consume_be<uint8_t>(p);
117 ah.oper = consume_be<uint16_t>(p);
118 ah.sender_hwaddr = l2addr::consume(p);
119 ah.sender_paddr = l3addr::consume(p);
120 ah.target_hwaddr = l2addr::consume(p);
121 ah.target_paddr = l3addr::consume(p);
124 void write(
char* p)
const {
125 produce_be<uint16_t>(p, htype);
126 produce_be<uint16_t>(p, ptype);
127 produce_be<uint8_t>(p, hlen);
128 produce_be<uint8_t>(p, plen);
129 produce_be<uint16_t>(p, oper);
130 sender_hwaddr.produce(p);
131 sender_paddr.produce(p);
132 target_hwaddr.produce(p);
133 target_paddr.produce(p);
135 static constexpr size_t size() {
136 return 8 + 2 * (l2addr::size() + l3addr::size());
140 std::vector<promise<l2addr>> _waiters;
144 l3addr _l3self = L3::broadcast_address();
145 std::unordered_map<l3addr, l2addr> _table;
146 std::unordered_map<l3addr, resolution> _in_progress;
148 packet make_query_packet(l3addr paddr);
150 future<> handle_request(arp_hdr* ah);
151 l2addr l2self()
const noexcept {
return _arp.l2self(); }
154 future<> send_query(
const l3addr& paddr);
156 _table[L3::broadcast_address()] = ethernet::broadcast_address();
159 void learn(
l2addr l2, l3addr l3);
161 void set_self_addr(l3addr addr) {
162 _table.erase(_l3self);
163 _table[addr] = l2self();
169template <
typename L3>
173 hdr.htype = ethernet::arp_hardware_type();
174 hdr.ptype = L3::arp_protocol_type();
175 hdr.hlen =
sizeof(l2addr);
176 hdr.plen =
sizeof(l3addr);
177 hdr.oper = op_request;
178 hdr.sender_hwaddr = l2self();
179 hdr.sender_paddr = _l3self;
180 hdr.target_hwaddr = ethernet::broadcast_address();
181 hdr.target_paddr = paddr;
183 p.prepend_uninitialized_header(hdr.size());
184 hdr.write(p.get_header(0, hdr.size()));
188template <
typename L3>
189void arp_for<L3>::send(l2addr to, packet p) {
190 _arp._packetq.push_back(l3_protocol::l3packet{eth_protocol_num::arp, to, std::move(p)});
193template <
typename L3>
195arp_for<L3>::send_query(
const l3addr& paddr) {
196 send(ethernet::broadcast_address(), make_query_packet(paddr));
197 return make_ready_future<>();
202 arp_error(
const std::string& msg) : std::runtime_error(msg) {}
215template <
typename L3>
218 auto i = _table.find(paddr);
219 if (i != _table.end()) {
220 return make_ready_future<ethernet_address>(i->second);
222 auto j = _in_progress.find(paddr);
223 auto first_request = j == _in_progress.end();
224 auto& res = first_request ? _in_progress[paddr] : j->second;
227 res._timeout_timer.set_callback([paddr,
this, &res] {
229 (void)send_query(paddr);
230 for (
auto& w : res._waiters) {
231 w.set_exception(arp_timeout_error());
233 res._waiters.clear();
235 res._timeout_timer.arm_periodic(std::chrono::seconds(1));
237 (void)send_query(paddr);
240 if (res._waiters.size() >= max_waiters) {
241 return make_exception_future<ethernet_address>(arp_queue_full_error());
244 res._waiters.emplace_back();
245 return res._waiters.back().get_future();
248template <
typename L3>
250arp_for<L3>::learn(l2addr hwaddr, l3addr paddr) {
251 _table[paddr] = hwaddr;
252 auto i = _in_progress.find(paddr);
253 if (i != _in_progress.end()) {
254 auto& res = i->second;
255 res._timeout_timer.cancel();
256 for (
auto &&pr : res._waiters) {
257 pr.set_value(hwaddr);
259 _in_progress.erase(i);
263template <
typename L3>
265arp_for<L3>::received(packet p) {
266 auto ah = p.get_header(0, arp_hdr::size());
268 return make_ready_future<>();
270 auto h = arp_hdr::read(ah);
271 if (h.hlen !=
sizeof(l2addr) || h.plen !=
sizeof(l3addr)) {
272 return make_ready_future<>();
276 return handle_request(&h);
278 arp_learn(h.sender_hwaddr, h.sender_paddr);
279 return make_ready_future<>();
281 return make_ready_future<>();
285template <
typename L3>
287arp_for<L3>::handle_request(arp_hdr* ah) {
288 if (ah->target_paddr == _l3self
289 && _l3self != L3::broadcast_address()) {
291 ah->target_hwaddr = ah->sender_hwaddr;
292 ah->target_paddr = ah->sender_paddr;
293 ah->sender_hwaddr = l2self();
294 ah->sender_paddr = _l3self;
296 ah->write(p.prepend_uninitialized_header(ah->size()));
297 send(ah->target_hwaddr, std::move(p));
299 return make_ready_future<>();
Definition: circular_buffer.hh:63
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: ethernet.hh:37