FD.io VPP  v19.01.3-6-g70449b9b9
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 void
21 to_api(const ip_address_t& a, vapi_type_address& v)
22 {
23  if (a.is_v4()) {
24  v.af = ADDRESS_IP4;
25  memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
26  } else {
27  v.af = ADDRESS_IP6;
28  memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
29  }
30 }
31 
32 void
34  vapi_union_address_union& u,
35  vapi_enum_address_family& af)
36 {
37  if (a.is_v4()) {
38  af = ADDRESS_IP4;
39  memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
40  } else {
41  af = ADDRESS_IP6;
42  memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
43  }
44 }
45 
46 void
47 to_api(const boost::asio::ip::address& a, vapi_type_ip4_address& v)
48 {
49  memcpy(v, a.to_v4().to_bytes().data(), 4);
50 }
51 
53 from_api(const vapi_type_address& v)
54 {
56 
57  if (ADDRESS_IP6 == v.af) {
58  std::array<uint8_t, 16> a;
59  std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
60  boost::asio::ip::address_v6 v6(a);
61  addr = v6;
62  } else {
63  std::array<uint8_t, 4> a;
64  std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
65  boost::asio::ip::address_v4 v4(a);
66  addr = v4;
67  }
68 
69  return addr;
70 }
71 
73 from_api(const vapi_union_address_union& u, vapi_enum_address_family af)
74 {
76 
77  if (ADDRESS_IP6 == af) {
78  std::array<uint8_t, 16> a;
79  std::copy(u.ip6, u.ip6 + 16, std::begin(a));
80  boost::asio::ip::address_v6 v6(a);
81  addr = v6;
82  } else {
83  std::array<uint8_t, 4> a;
84  std::copy(u.ip6, u.ip6 + 4, std::begin(a));
85  boost::asio::ip::address_v4 v4(a);
86  addr = v4;
87  }
88 
89  return addr;
90 }
91 
92 void
93 to_api(const mac_address_t& a, vapi_type_mac_address& v)
94 {
95  std::copy(std::begin(a.bytes), std::end(a.bytes), v);
96 }
97 
99 from_api(const vapi_type_mac_address& v)
100 {
101  return mac_address_t(v);
102 }
103 
105 from_api(const vapi_type_prefix& v)
106 {
107  return route::prefix_t(from_api(v.address), v.address_length);
108 }
109 
110 vapi_type_prefix
112 {
113  vapi_type_prefix v;
114  to_api(p.address(), v.address);
115  v.address_length = p.mask_width();
116  return v;
117 }
118 
120 from_api(const vapi_type_mprefix& v)
121 {
122  return route::mprefix_t(from_api(v.src_address, v.af),
123  from_api(v.grp_address, v.af), v.grp_address_length);
124 }
125 
126 vapi_type_mprefix
128 {
129  vapi_enum_address_family af;
130  vapi_type_mprefix v;
131  to_api(p.grp_address(), v.grp_address, af);
132  to_api(p.src_address(), v.src_address, af);
133  v.grp_address_length = p.mask_width();
134  v.af = af;
135  return v;
136 }
137 };
138 
139 /*
140  * fd.io coding-style-patch-verification: ON
141  *
142  * Local Variables:
143  * eval: (c-set-style "mozilla")
144  * End:
145  */
typedef address
Definition: ip_types.api:30
boost::asio::ip::address ip_address_t
Definition: api_types.hpp:24
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
ip_address_t from_api(const vapi_type_address &v)
Definition: api_types.cpp:53
vhost_vring_addr_t addr
Definition: vhost_user.h:121
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
const boost::asio::ip::address & src_address() const
Definition: prefix.cpp:484
void to_api(const ip_address_t &a, vapi_type_address &v)
Definition: api_types.cpp:21
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
Type def of a Ethernet address.
Definition: types.hpp:295
A prefix defintion.
Definition: prefix.hpp:92