Use IP and MAC API types for neighbors
[vpp.git] / src / vnet / ip / ip_types_api.c
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 void
33 ip6_address_encode (const ip6_address_t * in, vl_api_ip6_address_t out)
34 {
35   clib_memcpy (out, in, sizeof (*in));
36 }
37
38 void
39 ip6_address_decode (const vl_api_ip6_address_t in, ip6_address_t * out)
40 {
41   clib_memcpy (out, in, sizeof (*out));
42 }
43
44 void
45 ip4_address_encode (const ip4_address_t * in, vl_api_ip4_address_t out)
46 {
47   clib_memcpy (out, in, sizeof (*in));
48 }
49
50 void
51 ip4_address_decode (const vl_api_ip4_address_t in, ip4_address_t * out)
52 {
53   clib_memcpy (out, in, sizeof (*out));
54 }
55
56 static ip46_type_t
57 ip_address_union_decode (const vl_api_address_union_t * in,
58                          vl_api_address_family_t af, ip46_address_t * out)
59 {
60   ip46_type_t type;
61
62   switch (clib_net_to_host_u32 (af))
63     {
64     case ADDRESS_IP4:
65       clib_memset (out, 0, sizeof (*out));
66       clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
67       type = IP46_TYPE_IP4;
68       break;
69     case ADDRESS_IP6:
70       clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
71       type = IP46_TYPE_IP6;
72       break;
73     default:
74       ASSERT (!"Unkown address family in API address type");
75       type = IP46_TYPE_ANY;
76       break;
77     }
78
79   return type;
80 }
81
82 ip46_type_t
83 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
84 {
85   return (ip_address_union_decode (&in->un, in->af, out));
86 }
87
88 static void
89 ip_address_union_encode (const ip46_address_t * in,
90                          vl_api_address_family_t af,
91                          vl_api_address_union_t * out)
92 {
93   if (ADDRESS_IP6 == clib_net_to_host_u32 (af))
94     ip6_address_encode (&in->ip6, out->ip6);
95   else
96     ip4_address_encode (&in->ip4, out->ip4);
97 }
98
99 void
100 ip_address_encode (const ip46_address_t * in,
101                    ip46_type_t type, vl_api_address_t * out)
102 {
103   switch (type)
104     {
105     case IP46_TYPE_IP4:
106       out->af = clib_net_to_host_u32 (ADDRESS_IP4);
107       break;
108     case IP46_TYPE_IP6:
109       out->af = clib_net_to_host_u32 (ADDRESS_IP6);
110       break;
111     case IP46_TYPE_ANY:
112       if (ip46_address_is_ip4 (in))
113         out->af = clib_net_to_host_u32 (ADDRESS_IP4);
114       else
115         out->af = clib_net_to_host_u32 (ADDRESS_IP6);
116       break;
117     }
118   ip_address_union_encode (in, out->af, &out->un);
119 }
120
121 void
122 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
123 {
124   switch (clib_net_to_host_u32 (in->address.af))
125     {
126     case ADDRESS_IP4:
127       out->fp_proto = FIB_PROTOCOL_IP4;
128       break;
129     case ADDRESS_IP6:
130       out->fp_proto = FIB_PROTOCOL_IP6;
131       break;
132     }
133   out->fp_len = in->address_length;
134   out->___fp___pad = 0;
135   ip_address_decode (&in->address, &out->fp_addr);
136 }
137
138 void
139 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
140 {
141   out->address_length = in->fp_len;
142   ip_address_encode (&in->fp_addr,
143                      fib_proto_to_ip46 (in->fp_proto), &out->address);
144 }
145
146 void
147 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
148 {
149   out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
150   out->af = clib_host_to_net_u32 (out->af);
151   out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
152
153   ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
154   ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
155 }
156
157 void
158 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
159 {
160   out->fp_proto = (ADDRESS_IP6 == clib_net_to_host_u32 (in->af) ?
161                    FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
162   out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
163
164   ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
165   ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
166 }
167
168 /*
169  * fd.io coding-style-patch-verification: ON
170  *
171  * Local Variables:
172  * eval: (c-set-style "gnu")
173  * End:
174  */