FD.io VPP  v19.01.3-6-g70449b9b9
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 <vnet/ip/ip_types_api.h>
17 
18 #define vl_typedefs /* define message structures */
19 #include <vnet/vnet_all_api_h.h>
20 #undef vl_typedefs
21 
22 #define vl_endianfun /* define message structures */
23 #include <vnet/vnet_all_api_h.h>
24 #undef vl_endianfun
25 
26 /* instantiate all the print functions we know about */
27 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
28 #define vl_printfun
29 #include <vnet/vnet_all_api_h.h>
30 #undef vl_printfun
31 
32 static ip46_type_t
33 ip_address_union_decode (const vl_api_address_union_t * in,
34  vl_api_address_family_t af, ip46_address_t * out)
35 {
36  ip46_type_t type;
37 
38  switch (clib_net_to_host_u32 (af))
39  {
40  case ADDRESS_IP4:
41  clib_memset (out, 0, sizeof (*out));
42  clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
43  type = IP46_TYPE_IP4;
44  break;
45  case ADDRESS_IP6:
46  clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
47  type = IP46_TYPE_IP6;
48  break;
49  default:
50  ASSERT (!"Unkown address family in API address type");
51  type = IP46_TYPE_ANY;
52  break;
53  }
54 
55  return type;
56 }
57 
59 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
60 {
61  return (ip_address_union_decode (&in->un, in->af, out));
62 }
63 
64 static void
65 ip_address_union_encode (const ip46_address_t * in,
66  vl_api_address_family_t af,
67  vl_api_address_union_t * out)
68 {
69  if (ADDRESS_IP6 == clib_net_to_host_u32 (af))
70  memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
71  else
72  memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
73 }
74 
75 void
76 ip_address_encode (const ip46_address_t * in,
77  ip46_type_t type, vl_api_address_t * out)
78 {
79  switch (type)
80  {
81  case IP46_TYPE_IP4:
82  out->af = clib_net_to_host_u32 (ADDRESS_IP4);
83  break;
84  case IP46_TYPE_IP6:
85  out->af = clib_net_to_host_u32 (ADDRESS_IP6);
86  break;
87  case IP46_TYPE_ANY:
88  if (ip46_address_is_ip4 (in))
89  out->af = clib_net_to_host_u32 (ADDRESS_IP4);
90  else
91  out->af = clib_net_to_host_u32 (ADDRESS_IP6);
92  break;
93  }
94  ip_address_union_encode (in, out->af, &out->un);
95 }
96 
97 void
98 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
99 {
100  switch (clib_net_to_host_u32 (in->address.af))
101  {
102  case ADDRESS_IP4:
103  out->fp_proto = FIB_PROTOCOL_IP4;
104  break;
105  case ADDRESS_IP6:
106  out->fp_proto = FIB_PROTOCOL_IP6;
107  break;
108  }
109  out->fp_len = in->address_length;
110  out->___fp___pad = 0;
111  ip_address_decode (&in->address, &out->fp_addr);
112 }
113 
114 void
115 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
116 {
117  out->address_length = in->fp_len;
119  fib_proto_to_ip46 (in->fp_proto), &out->address);
120 }
121 
122 void
123 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
124 {
125  out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
126  out->af = clib_host_to_net_u32 (out->af);
127  out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
128 
129  ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
130  ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
131 }
132 
133 void
134 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
135 {
136  out->fp_proto = (ADDRESS_IP6 == clib_net_to_host_u32 (in->af) ?
138  out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
139 
140  ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
141  ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
142 }
143 
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "gnu")
149  * End:
150  */
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:33
void ip_prefix_decode(const vl_api_prefix_t *in, fib_prefix_t *out)
Definition: ip_types_api.c:98
ip46_type_t fib_proto_to_ip46(fib_protocol_t fproto)
Convert from fib_protocol to ip46_type.
Definition: fib_types.c:287
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define clib_memcpy(d, s, n)
Definition: string.h:180
Aggregrate 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:59
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:88
void ip_mprefix_encode(const mfib_prefix_t *in, vl_api_mprefix_t *out)
Definition: ip_types_api.c:123
Aggregrate type for a prefix.
Definition: mfib_types.h:24
#define ASSERT(truth)
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:65
void ip_prefix_encode(const fib_prefix_t *in, vl_api_prefix_t *out)
Definition: ip_types_api.c:115
fib_protocol_t fp_proto
protocol type
Definition: mfib_types.h:33
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:76
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:134
ip46_address_t fp_grp_addr
The address type is not deriveable from the fp_addr member.
Definition: mfib_types.h:46