FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
api_types.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vom/api_types.hpp>
17 
18 namespace VOM {
19 
20 static vapi_type_ip4_address
21 to_api(const boost::asio::ip::address_v4& a)
22 {
23  vapi_type_ip4_address v;
24 
25  std::copy_n(a.to_bytes().data(), 4, v.address);
26 
27  return v;
28 }
29 
30 static vapi_type_ip6_address
31 to_api(const boost::asio::ip::address_v6& a)
32 {
33  vapi_type_ip6_address v;
34 
35  std::copy_n(a.to_bytes().data(), 16, v.address);
36 
37  return v;
38 }
39 
40 vapi_type_address
42 {
43  if (a.is_v4()) {
44  vapi_type_address v = {
45  .af = ADDRESS_IP4,
46  .un =
47  {
48  .ip4 = to_api(a.to_v4()),
49  },
50  };
51  return (v);
52  } else {
53  vapi_type_address v = {
54  .af = ADDRESS_IP6,
55  .un =
56  {
57  .ip6 = to_api(a.to_v6()),
58  },
59  };
60  return (v);
61  }
62 }
63 
65 from_api(const vapi_type_address& v)
66 {
68 
69  if (ADDRESS_IP6 == v.af) {
70  std::array<uint8_t, 16> a;
71  std::copy(v.un.ip6.address, v.un.ip6.address + 16, std::begin(a));
72  boost::asio::ip::address_v6 v6(a);
73  addr = v6;
74  } else {
75  std::array<uint8_t, 4> a;
76  std::copy(v.un.ip6.address, v.un.ip6.address + 4, std::begin(a));
77  boost::asio::ip::address_v4 v4(a);
78  addr = v4;
79  }
80 
81  return addr;
82 }
83 
84 vapi_type_mac_address
86 {
87  vapi_type_mac_address v;
88 
89  std::copy(std::begin(a.bytes), std::end(a.bytes), v.bytes);
90 
91  return (v);
92 }
93 
95 from_api(const vapi_type_mac_address& v)
96 {
97  return mac_address_t(v.bytes);
98 }
99 
101 from_api(const vapi_type_prefix& v)
102 {
103  return route::prefix_t(from_api(v.address), v.address_length);
104 }
105 
106 vapi_type_prefix
108 {
109  vapi_type_prefix v = {
110  .address = to_api(p.address()), .address_length = p.mask_width(),
111  };
112 
113  return v;
114 }
115 };
116 
117 /*
118  * fd.io coding-style-patch-verification: ON
119  *
120  * Local Variables:
121  * eval: (c-set-style "mozilla")
122  * End:
123  */
typedef address
Definition: ip_types.api:35
boost::asio::ip::address ip_address_t
Definition: api_types.hpp:24
a
Definition: bitmap.h:538
ip_address_t from_api(const vapi_type_address &v)
Definition: api_types.cpp:65
vhost_vring_addr_t addr
Definition: vhost_user.h:121
u8 address_length
Definition: ip_types.api:42
#define v
Definition: acl.c:496
struct mac_address_t_ mac_address_t
std::array< uint8_t, 6 > bytes
Underlying bytes array.
Definition: types.hpp:328
static vapi_type_ip4_address to_api(const boost::asio::ip::address_v4 &a)
Definition: api_types.cpp:21
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
uint8_t mask_width() const
Get the network mask width.
Definition: prefix.cpp:155
const boost::asio::ip::address & address() const
Get the address.
Definition: prefix.cpp:149
Type def of a Ethernet address.
Definition: types.hpp:290
A prefix defintion.
Definition: prefix.hpp:92