Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / ip_types.h
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 /*
24  * Ceph - scalable distributed file system
25  *
26  * Copyright (C) 2015 XSky <haomai@xsky.com>
27  *
28  * Author: Haomai Wang <haomaiwang@gmail.com>
29  *
30  * This is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Lesser General Public
32  * License version 2.1, as published by the Free Software
33  * Foundation.  See file COPYING.
34  *
35  */
36
37 #ifndef CEPH_IP_TYPES_H_H
38 #define CEPH_IP_TYPES_H_H
39
40 #include <boost/asio/ip/address_v4.hpp>
41 #include <string>
42
43 class Packet;
44 class ethernet_address;
45 using resolution_cb = std::function<void (const ethernet_address&, Packet, int)>;
46
47 struct ipv4_addr {
48   uint32_t ip;
49   uint16_t port;
50
51   ipv4_addr() : ip(0), port(0) {}
52   ipv4_addr(uint32_t ip, uint16_t port) : ip(ip), port(port) {}
53   ipv4_addr(uint16_t port) : ip(0), port(port) {}
54   ipv4_addr(const std::string &addr);
55   ipv4_addr(const std::string &addr, uint16_t port);
56
57   ipv4_addr(const entity_addr_t &ad) {
58     ip = ntoh(ad.in4_addr().sin_addr.s_addr);
59     port = ad.get_port();
60   }
61
62   ipv4_addr(entity_addr_t &&addr) : ipv4_addr(addr) {}
63 };
64
65 struct ipv4_address {
66   ipv4_address() : ip(0) {}
67   explicit ipv4_address(uint32_t ip) : ip(ip) {}
68   explicit ipv4_address(const std::string& addr) {
69     ip = static_cast<uint32_t>(boost::asio::ip::address_v4::from_string(addr).to_ulong());
70   }
71   ipv4_address(ipv4_addr addr) {
72     ip = addr.ip;
73   }
74
75   uint32_t ip;
76
77   ipv4_address hton() {
78     ipv4_address addr;
79     addr.ip = ::hton(ip);
80     return addr;
81   }
82   ipv4_address ntoh() {
83     ipv4_address addr;
84     addr.ip = ::ntoh(ip);
85     return addr;
86   }
87
88   friend bool operator==(ipv4_address x, ipv4_address y) {
89     return x.ip == y.ip;
90   }
91   friend bool operator!=(ipv4_address x, ipv4_address y) {
92     return x.ip != y.ip;
93   }
94 } __attribute__((packed));
95
96 static inline bool is_unspecified(ipv4_address addr) { return addr.ip == 0; }
97
98 std::ostream& operator<<(std::ostream& os, const ipv4_address& a);
99
100 namespace std {
101
102   template <>
103   struct hash<ipv4_address> {
104     size_t operator()(ipv4_address a) const { return a.ip; }
105   };
106
107 }
108
109 #endif //CEPH_IP_TYPES_H_H