FD.io VPP  v19.04.4-rc0-5-ge88582fac
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 vapi_enum_ip_neighbor_flags
22 {
23  vapi_enum_ip_neighbor_flags out = IP_API_NEIGHBOR_FLAG_NONE;
24 
26  out = static_cast<vapi_enum_ip_neighbor_flags>(out |
29  out = static_cast<vapi_enum_ip_neighbor_flags>(
31 
32  return (out);
33 }
34 
36 from_api(vapi_enum_ip_neighbor_flags f)
37 {
39 
44 
45  return out;
46 }
47 
48 void
49 to_api(const boost::asio::ip::address_v4& a, vapi_type_ip4_address& v)
50 {
51  std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
52 }
53 void
54 to_api(const boost::asio::ip::address_v6& a, vapi_type_ip6_address& v)
55 {
56  std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
57 }
58 
59 void
60 to_api(const ip_address_t& a, vapi_type_address& v)
61 {
62  if (a.is_v4()) {
63  v.af = ADDRESS_IP4;
64  memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
65  } else {
66  v.af = ADDRESS_IP6;
67  memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
68  }
69 }
70 
71 void
73  vapi_union_address_union& u,
74  vapi_enum_address_family& af)
75 {
76  if (a.is_v4()) {
77  af = ADDRESS_IP4;
78  memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
79  } else {
80  af = ADDRESS_IP6;
81  memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
82  }
83 }
84 
85 boost::asio::ip::address_v6
86 from_api(const vapi_type_ip6_address& v)
87 {
88  std::array<uint8_t, 16> a;
89  std::copy(v, v + 16, std::begin(a));
90  boost::asio::ip::address_v6 v6(a);
91 
92  return v6;
93 }
94 
95 boost::asio::ip::address_v4
96 from_api(const vapi_type_ip4_address& v)
97 {
98  std::array<uint8_t, 4> a;
99  std::copy(v, v + 4, std::begin(a));
100  boost::asio::ip::address_v4 v4(a);
101 
102  return v4;
103 }
104 
106 from_api(const vapi_type_address& v)
107 {
109 
110  if (ADDRESS_IP6 == v.af) {
111  std::array<uint8_t, 16> a;
112  std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
113  boost::asio::ip::address_v6 v6(a);
114  addr = v6;
115  } else {
116  std::array<uint8_t, 4> a;
117  std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
118  boost::asio::ip::address_v4 v4(a);
119  addr = v4;
120  }
121 
122  return addr;
123 }
124 
126 from_api(const vapi_union_address_union& u, vapi_enum_address_family af)
127 {
129 
130  if (ADDRESS_IP6 == af) {
131  std::array<uint8_t, 16> a;
132  std::copy(u.ip6, u.ip6 + 16, std::begin(a));
133  boost::asio::ip::address_v6 v6(a);
134  addr = v6;
135  } else {
136  std::array<uint8_t, 4> a;
137  std::copy(u.ip6, u.ip6 + 4, std::begin(a));
138  boost::asio::ip::address_v4 v4(a);
139  addr = v4;
140  }
141 
142  return addr;
143 }
144 
145 void
146 to_api(const mac_address_t& a, vapi_type_mac_address& v)
147 {
148  std::copy(std::begin(a.bytes), std::end(a.bytes), v);
149 }
150 
152 from_api(const vapi_type_mac_address& v)
153 {
154  return mac_address_t(v);
155 }
156 
158 from_api(const vapi_type_prefix& v)
159 {
160  return route::prefix_t(from_api(v.address), v.address_length);
161 }
162 
163 vapi_type_prefix
165 {
166  vapi_type_prefix v;
167  to_api(p.address(), v.address);
168  v.address_length = p.mask_width();
169  return v;
170 }
171 
173 from_api(const vapi_type_mprefix& v)
174 {
175  return route::mprefix_t(from_api(v.src_address, v.af),
176  from_api(v.grp_address, v.af), v.grp_address_length);
177 }
178 
179 vapi_type_mprefix
181 {
182  vapi_enum_address_family af;
183  vapi_type_mprefix v;
184  to_api(p.grp_address(), v.grp_address, af);
185  to_api(p.src_address(), v.src_address, af);
186  v.grp_address_length = p.mask_width();
187  v.af = af;
188  return v;
189 }
190 };
191 
192 /*
193  * fd.io coding-style-patch-verification: ON
194  *
195  * Local Variables:
196  * eval: (c-set-style "mozilla")
197  * End:
198  */
typedef address
Definition: ip_types.api:30
boost::asio::ip::address ip_address_t
Definition: api_types.hpp:25
a
Definition: bitmap.h:538
const boost::asio::ip::address & address() const
Get the address.
Definition: prefix.cpp:149
const boost::asio::ip::address & grp_address() const
Get the address.
Definition: prefix.cpp:478
static const flags_t STATIC
Definition: neighbour.hpp:50
vhost_vring_addr_t addr
Definition: vhost_user.h:121
vapi_enum_ip_neighbor_flags to_api(const neighbour::flags_t &f)
Definition: api_types.cpp:21
uint8_t mask_width() const
Get the network mask width.
Definition: prefix.cpp:490
uint8_t mask_width() const
Get the network mask width.
Definition: prefix.cpp:155
static const flags_t NONE
Definition: neighbour.hpp:49
const boost::asio::ip::address & src_address() const
Definition: prefix.cpp:484
static const flags_t NO_FIB_ENTRY
Definition: neighbour.hpp:51
struct mac_address_t_ mac_address_t
std::array< uint8_t, 6 > bytes
Underlying bytes array.
Definition: types.hpp:333
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A prefix defintion.
Definition: prefix.hpp:213
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
Type def of a Ethernet address.
Definition: types.hpp:295
A prefix defintion.
Definition: prefix.hpp:92