FD.io VPP  v19.08.3-2-gbabecb413
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 invalid_decode::invalid_decode(const std::string reason)
49  : reason(reason)
50 {
51 }
52 
53 void
54 to_api(const boost::asio::ip::address_v4& a, vapi_type_ip4_address& v)
55 {
56  std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
57 }
58 void
59 to_api(const boost::asio::ip::address_v6& a, vapi_type_ip6_address& v)
60 {
61  std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
62 }
63 
64 void
65 to_api(const ip_address_t& a, vapi_type_address& v)
66 {
67  if (a.is_v4()) {
68  v.af = ADDRESS_IP4;
69  memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
70  } else {
71  v.af = ADDRESS_IP6;
72  memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
73  }
74 }
75 
76 void
78  vapi_union_address_union& u,
79  vapi_enum_address_family& af)
80 {
81  if (a.is_v4()) {
82  af = ADDRESS_IP4;
83  memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
84  } else {
85  af = ADDRESS_IP6;
86  memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
87  }
88 }
89 
90 void
91 to_api(const ip_address_t& a, vapi_union_address_union& u)
92 {
93  if (a.is_v4()) {
94  memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
95  } else {
96  memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
97  }
98 }
99 
100 boost::asio::ip::address_v6
101 from_api(const vapi_type_ip6_address& v)
102 {
103  std::array<uint8_t, 16> a;
104  std::copy(v, v + 16, std::begin(a));
105  boost::asio::ip::address_v6 v6(a);
106 
107  return v6;
108 }
109 
110 boost::asio::ip::address_v4
111 from_api(const vapi_type_ip4_address& v)
112 {
113  std::array<uint8_t, 4> a;
114  std::copy(v, v + 4, std::begin(a));
115  boost::asio::ip::address_v4 v4(a);
116 
117  return v4;
118 }
119 
121 from_api(const vapi_type_address& v)
122 {
124 
125  if (ADDRESS_IP6 == v.af) {
126  std::array<uint8_t, 16> a;
127  std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
128  boost::asio::ip::address_v6 v6(a);
129  addr = v6;
130  } else {
131  std::array<uint8_t, 4> a;
132  std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
133  boost::asio::ip::address_v4 v4(a);
134  addr = v4;
135  }
136 
137  return addr;
138 }
139 
141 from_api(const vapi_union_address_union& u, vapi_enum_fib_path_nh_proto proto)
142 {
144 
145  if (FIB_API_PATH_NH_PROTO_IP6 == proto) {
146  std::array<uint8_t, 16> a;
147  std::copy(u.ip6, u.ip6 + 16, std::begin(a));
148  boost::asio::ip::address_v6 v6(a);
149  addr = v6;
150  } else if (FIB_API_PATH_NH_PROTO_IP4 == proto) {
151  std::array<uint8_t, 4> a;
152  std::copy(u.ip6, u.ip6 + 4, std::begin(a));
153  boost::asio::ip::address_v4 v4(a);
154  addr = v4;
155  }
156 
157  return addr;
158 }
159 
161 from_api(const vapi_union_address_union& u, vapi_enum_address_family af)
162 {
164 
165  if (ADDRESS_IP6 == af) {
166  std::array<uint8_t, 16> a;
167  std::copy(u.ip6, u.ip6 + 16, std::begin(a));
168  boost::asio::ip::address_v6 v6(a);
169  addr = v6;
170  } else {
171  std::array<uint8_t, 4> a;
172  std::copy(u.ip6, u.ip6 + 4, std::begin(a));
173  boost::asio::ip::address_v4 v4(a);
174  addr = v4;
175  }
176 
177  return addr;
178 }
179 
180 void
181 to_api(const mac_address_t& a, vapi_type_mac_address& v)
182 {
183  std::copy(std::begin(a.bytes), std::end(a.bytes), v);
184 }
185 
187 from_api(const vapi_type_mac_address& v)
188 {
189  return mac_address_t(v);
190 }
191 
193 from_api(const vapi_type_prefix& v)
194 {
195  return route::prefix_t(from_api(v.address), v.len);
196 }
197 
198 vapi_type_prefix
200 {
201  vapi_type_prefix v;
202  to_api(p.address(), v.address);
203  v.len = p.mask_width();
204  return v;
205 }
206 
208 from_api(const vapi_type_mprefix& v)
209 {
210  return route::mprefix_t(from_api(v.src_address, v.af),
211  from_api(v.grp_address, v.af), v.grp_address_length);
212 }
213 
214 vapi_type_mprefix
216 {
217  vapi_enum_address_family af;
218  vapi_type_mprefix v;
219  to_api(p.grp_address(), v.grp_address, af);
220  to_api(p.src_address(), v.src_address, af);
221  v.grp_address_length = p.mask_width();
222  v.af = af;
223  return v;
224 }
225 
226 vapi_enum_fib_path_nh_proto
228 {
229  if (p == nh_proto_t::IPV4) {
231  } else if (p == nh_proto_t::IPV6) {
233  } else if (p == nh_proto_t::ETHERNET) {
235  } else if (p == nh_proto_t::MPLS) {
237  }
238 
240 }
241 const nh_proto_t&
242 from_api(vapi_enum_fib_path_nh_proto p)
243 {
244  switch (p) {
246  return nh_proto_t::IPV4;
248  return nh_proto_t::IPV6;
250  return nh_proto_t::ETHERNET;
252  return nh_proto_t::MPLS;
254  break;
255  }
256 
257  return nh_proto_t::IPV4;
258 }
259 
260 }; // VOM
261 
262 /*
263  * fd.io coding-style-patch-verification: ON
264  *
265  * Local Variables:
266  * eval: (c-set-style "mozilla")
267  * End:
268  */
typedef address
Definition: ip_types.api:83
boost::asio::ip::address ip_address_t
Definition: api_types.hpp:31
a
Definition: bitmap.h:538
const boost::asio::ip::address & address() const
Get the address.
Definition: prefix.cpp:180
static const nh_proto_t IPV6
Definition: prefix.hpp:35
static const nh_proto_t IPV4
Definition: prefix.hpp:34
const boost::asio::ip::address & grp_address() const
Get the address.
Definition: prefix.cpp:509
static const flags_t STATIC
Definition: neighbour.hpp:50
vhost_vring_addr_t addr
Definition: vhost_user.h:147
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:521
uint8_t mask_width() const
Get the network mask width.
Definition: prefix.cpp:186
Types belonging to Routing.
Definition: prefix.hpp:31
static const flags_t NONE
Definition: neighbour.hpp:49
const boost::asio::ip::address & src_address() const
Definition: prefix.cpp:515
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
invalid_decode(const std::string reason)
Definition: api_types.cpp:48
static const nh_proto_t MPLS
Definition: prefix.hpp:36
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A prefix defintion.
Definition: prefix.hpp:252
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:131
static const nh_proto_t ETHERNET
Definition: prefix.hpp:37
vl_api_fib_path_nh_proto_t proto
Definition: fib_types.api:125