FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
ip_types_api.c
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 <vlibapi/api_types.h>
17 #include <vnet/ip/ip_types_api.h>
18 
19 #define vl_typedefs /* define message structures */
20 #include <vnet/vnet_all_api_h.h>
21 #undef vl_typedefs
22 
23 #define vl_endianfun /* define message structures */
24 #include <vnet/vnet_all_api_h.h>
25 #undef vl_endianfun
26 
27 /* instantiate all the print functions we know about */
28 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
29 #define vl_printfun
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_printfun
32 
33 int
35 {
36  vl_api_address_family_t af = clib_host_to_net_u32 (_af);
37 
38  switch (af)
39  {
40  case ADDRESS_IP4:
41  *out = AF_IP4;
42  return (0);
43  case ADDRESS_IP6:
44  *out = AF_IP6;
45  return (0);
46  }
47  return (-1);
48 }
49 
50 int
52 {
53  switch (af)
54  {
55  case AF_IP4:
56  return (clib_host_to_net_u32 (ADDRESS_IP4));
57  case AF_IP6:
58  return (clib_host_to_net_u32 (ADDRESS_IP6));
59  }
60 
61  ASSERT (0);
62  return (clib_host_to_net_u32 (ADDRESS_IP4));
63 }
64 
65 int
66 ip_proto_decode (int _ipp, ip_protocol_t * out)
67 {
68  ip_protocol_t ipp = clib_host_to_net_u32 (_ipp);
69 
70  switch (ipp)
71  {
72 #define ip_protocol(n,s) \
73  case IP_PROTOCOL_##s: \
74  *out = IP_PROTOCOL_##s; \
75  return (0);
76 #include "protocols.def"
77 #undef ip_protocol
78  }
79  return (-1);
80 }
81 
82 int
84 {
85  switch (ipp)
86  {
87 #define ip_protocol(n,s) \
88  case IP_PROTOCOL_##s: \
89  return (clib_host_to_net_u32 (IP_PROTOCOL_##s));
90 #include "protocols.def"
91 #undef ip_protocol
92  }
93 
94  ASSERT (0);
95  return (clib_host_to_net_u32 (IP_API_PROTO_TCP));
96 }
97 
100 {
101  return ((ip_dscp_t) in);
102 }
103 
104 u8
106 {
107  return (dscp);
108 }
109 
110 void
112 {
113  clib_memcpy (out, in, sizeof (*in));
114 }
115 
116 void
118 {
119  clib_memcpy (out, in, sizeof (*out));
120 }
121 
122 void
124 {
125  clib_memcpy (out, in, sizeof (*in));
126 }
127 
128 void
130 {
131  clib_memcpy (out, in, sizeof (*out));
132 }
133 
134 static ip46_type_t
135 ip_address_union_decode (const vl_api_address_union_t * in,
136  vl_api_address_family_t af, ip46_address_t * out)
137 {
139 
140  switch (clib_net_to_host_u32 (af))
141  {
142  case ADDRESS_IP4:
143  clib_memset (out, 0, sizeof (*out));
144  clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
145  type = IP46_TYPE_IP4;
146  break;
147  case ADDRESS_IP6:
148  clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
149  type = IP46_TYPE_IP6;
150  break;
151  default:
152  ASSERT (!"Unknown address family in API address type");
153  type = IP46_TYPE_ANY;
154  break;
155  }
156 
157  return type;
158 }
159 
161 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
162 {
163  return (ip_address_union_decode (&in->un, in->af, out));
164 }
165 
166 static void
167 ip_address_union_encode (const ip46_address_t * in,
168  vl_api_address_family_t af,
169  vl_api_address_union_t * out)
170 {
171  if (ADDRESS_IP6 == clib_net_to_host_u32 (af))
172  ip6_address_encode (&in->ip6, out->ip6);
173  else
174  ip4_address_encode (&in->ip4, out->ip4);
175 }
176 
177 void
178 ip_address_encode (const ip46_address_t * in,
179  ip46_type_t type, vl_api_address_t * out)
180 {
181  switch (type)
182  {
183  case IP46_TYPE_IP4:
184  out->af = clib_net_to_host_u32 (ADDRESS_IP4);
185  break;
186  case IP46_TYPE_IP6:
187  out->af = clib_net_to_host_u32 (ADDRESS_IP6);
188  break;
189  case IP46_TYPE_ANY:
190  if (ip46_address_is_ip4 (in))
191  out->af = clib_net_to_host_u32 (ADDRESS_IP4);
192  else
193  out->af = clib_net_to_host_u32 (ADDRESS_IP6);
194  break;
195  }
196  ip_address_union_encode (in, out->af, &out->un);
197 }
198 
199 void
200 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
201 {
202  switch (clib_net_to_host_u32 (in->address.af))
203  {
204  case ADDRESS_IP4:
205  out->fp_proto = FIB_PROTOCOL_IP4;
206  break;
207  case ADDRESS_IP6:
208  out->fp_proto = FIB_PROTOCOL_IP6;
209  break;
210  }
211  out->fp_len = in->len;
212  out->___fp___pad = 0;
213  ip_address_decode (&in->address, &out->fp_addr);
214 }
215 
216 void
217 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
218 {
219  out->len = in->fp_len;
220  ip46_type_t ip46_type;
221 
222  switch (in->fp_proto)
223  {
224  case FIB_PROTOCOL_IP4:
225  ip46_type = (IP46_TYPE_IP4);
226  break;
227  case FIB_PROTOCOL_IP6:
228  ip46_type = (IP46_TYPE_IP6);
229  break;
230  case FIB_PROTOCOL_MPLS:
231  ip46_type = (IP46_TYPE_ANY);
232  break;
233  default:
234  ip46_type = (IP46_TYPE_ANY);
235  }
236 
237  ip_address_encode (&in->fp_addr, ip46_type, &out->address);
238 }
239 
240 void
241 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
242 {
243  out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
244  out->af = clib_host_to_net_u32 (out->af);
245  out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
246 
247  ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
248  ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
249 }
250 
251 void
252 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
253 {
254  out->fp_proto = (ADDRESS_IP6 == clib_net_to_host_u32 (in->af) ?
256  out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
257  out->___fp___pad = 0;
258 
259  ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
260  ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
261 
262  if (!ip46_address_is_zero (&out->fp_src_addr))
263  out->fp_len = (out->fp_proto == FIB_PROTOCOL_IP6 ? 256 : 64);
264 }
265 
266 /*
267  * fd.io coding-style-patch-verification: ON
268  *
269  * Local Variables:
270  * eval: (c-set-style "gnu")
271  * End:
272  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
ip46_address_t fp_src_addr
Definition: mfib_types.h:47
static ip46_type_t ip_address_union_decode(const vl_api_address_union_t *in, vl_api_address_family_t af, ip46_address_t *out)
Definition: ip_types_api.c:135
void ip_prefix_decode(const vl_api_prefix_t *in, fib_prefix_t *out)
Definition: ip_types_api.c:200
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u8 vl_api_ip6_address_t[16]
Conversion functions to/from (decode/encode) API types to VPP internal types.
Definition: ip_types_api.h:30
void ip6_address_decode(const vl_api_ip6_address_t in, ip6_address_t *out)
Definition: ip_types_api.c:117
enum ip_address_family_t_ ip_address_family_t
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
int ip_proto_decode(int _ipp, ip_protocol_t *out)
Definition: ip_types_api.c:66
Aggregate type for a prefix.
Definition: fib_types.h:203
u16 fp_len
The mask length.
Definition: fib_types.h:207
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Definition: ip_types_api.c:161
ip_dscp_t ip_dscp_decode(u8 in)
Definition: ip_types_api.c:99
vl_api_fib_path_type_t type
Definition: fib_types.api:123
int ip_address_family_encode(ip_address_family_t af)
Definition: ip_types_api.c:51
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
enum ip_protocol ip_protocol_t
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:88
u8 vl_api_ip4_address_t[4]
Definition: ip_types_api.h:31
void ip_mprefix_encode(const mfib_prefix_t *in, vl_api_mprefix_t *out)
Definition: ip_types_api.c:241
Aggregate type for a prefix.
Definition: mfib_types.h:24
int ip_proto_encode(ip_protocol_t ipp)
Definition: ip_types_api.c:83
#define ASSERT(truth)
enum ip_dscp_t_ ip_dscp_t
ip46_type_t
Definition: ip6_packet.h:70
static void ip_address_union_encode(const ip46_address_t *in, vl_api_address_family_t af, vl_api_address_union_t *out)
Definition: ip_types_api.c:167
void ip6_address_encode(const ip6_address_t *in, vl_api_ip6_address_t out)
Definition: ip_types_api.c:111
void ip_prefix_encode(const fib_prefix_t *in, vl_api_prefix_t *out)
Definition: ip_types_api.c:217
fib_protocol_t fp_proto
protocol type
Definition: mfib_types.h:33
void ip4_address_decode(const vl_api_ip4_address_t in, ip4_address_t *out)
Definition: ip_types_api.c:129
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:178
int ip_address_family_decode(int _af, ip_address_family_t *out)
These enum decode/encodes use &#39;int&#39; as the type for the enum because one cannot forward declare an en...
Definition: ip_types_api.c:34
u8 ip_dscp_encode(ip_dscp_t dscp)
Definition: ip_types_api.c:105
void ip4_address_encode(const ip4_address_t *in, vl_api_ip4_address_t out)
Definition: ip_types_api.c:123
u16 fp_len
The mask length.
Definition: mfib_types.h:28
void ip_mprefix_decode(const vl_api_mprefix_t *in, mfib_prefix_t *out)
Definition: ip_types_api.c:252
#define ip46_address_is_zero(ip46)
Definition: ip6_packet.h:93
ip46_address_t fp_grp_addr
The address type is not deriveable from the fp_addr member.
Definition: mfib_types.h:46