Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / ARP.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 /*
3  * This file is open source software, licensed to you under the terms
4  * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
5  * distributed with this work for additional information regarding copyright
6  * ownership.  You may not use this file except in compliance with the License.
7  *
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 /*
20  * Copyright (C) 2014 Cloudius Systems, Ltd.
21  */
22 /*
23  * Ceph - scalable distributed file system
24  *
25  * Copyright (C) 2015 XSky <haomai@xsky.com>
26  *
27  * Author: Haomai Wang <haomaiwang@gmail.com>
28  *
29  * This is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Lesser General Public
31  * License version 2.1, as published by the Free Software
32  * Foundation.  See file COPYING.
33  *
34  */
35
36 #include "ARP.h"
37
38 arp_for_protocol::arp_for_protocol(arp& a, uint16_t proto_num)
39     : _arp(a), _proto_num(proto_num)
40 {
41   _arp.add(proto_num, this);
42 }
43
44 arp_for_protocol::~arp_for_protocol()
45 {
46   _arp.del(_proto_num);
47 }
48
49 arp::arp(interface* netif):
50     _netif(netif),
51     _proto(netif, eth_protocol_num::arp, [this] { return get_packet(); }),
52     _rx_packets(
53         _proto.receive(
54             [this] (Packet p, ethernet_address ea) {
55               return process_packet(std::move(p), ea);
56             },
57             [this](forward_hash& out_hash_data, Packet& p, size_t off) {
58               return forward(out_hash_data, p, off);
59             }
60         )
61     )
62 {}
63
64 Tub<l3_protocol::l3packet> arp::get_packet()
65 {
66   Tub<l3_protocol::l3packet> p;
67   if (!_packetq.empty()) {
68     p = std::move(_packetq.front());
69     _packetq.pop_front();
70   }
71   return p;
72 }
73
74 bool arp::forward(forward_hash& out_hash_data, Packet& p, size_t off)
75 {
76   auto ah = p.get_header<arp_hdr>(off);
77   auto i = _arp_for_protocol.find(ntoh(ah->ptype));
78   if (i != _arp_for_protocol.end()) {
79     return i->second->forward(out_hash_data, p, off);
80   }
81   return false;
82 }
83
84 void arp::add(uint16_t proto_num, arp_for_protocol* afp)
85 {
86   _arp_for_protocol[proto_num] = afp;
87 }
88
89 void arp::del(uint16_t proto_num)
90 {
91   _arp_for_protocol.erase(proto_num);
92 }
93
94 int arp::process_packet(Packet p, ethernet_address from)
95 {
96   auto ah = p.get_header<arp_hdr>()->ntoh();
97   auto i = _arp_for_protocol.find(ah.ptype);
98   if (i != _arp_for_protocol.end()) {
99     i->second->received(std::move(p));
100   }
101   return 0;
102 }