FD.io VPP  v21.01.1
Vector Packet Processing
udp_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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/vnet.h>
17 #include <vlibmemory/api.h>
18 
19 #include <vnet/udp/udp_encap.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/ip/ip_types_api.h>
22 
23 #include <vnet/vnet_msg_enum.h>
24 
25 #define vl_typedefs /* define message structures */
26 #include <vnet/vnet_all_api_h.h>
27 #undef vl_typedefs
28 
29 #define vl_endianfun /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_endianfun
32 
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35 #define vl_printfun
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_printfun
38 
40 
41 
42 #define foreach_udp_api_msg \
43 _(UDP_ENCAP_DEL, udp_encap_del) \
44 _(UDP_ENCAP_ADD, udp_encap_add) \
45 _(UDP_ENCAP_DUMP, udp_encap_dump)
46 
47 static void
49  u32 context)
50 {
52 
53  mp = vl_msg_api_alloc (sizeof (*mp));
54  clib_memset (mp, 0, sizeof (*mp));
55  mp->_vl_msg_id = ntohs (VL_API_UDP_ENCAP_DETAILS);
56  mp->context = context;
57 
58  if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
59  {
60  clib_memcpy (&mp->udp_encap.src_ip.un.ip4,
61  &ue->ue_hdrs.ip4.ue_ip4.src_address, 4);
62  clib_memcpy (&mp->udp_encap.dst_ip.un.ip4,
63  &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4);
64  mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP4);
65  mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP4);
66 
67  /* ports aren't byte swapped because they are stored in network
68  * byte order */
69  mp->udp_encap.src_port = ue->ue_hdrs.ip4.ue_udp.src_port;
70  mp->udp_encap.dst_port = ue->ue_hdrs.ip4.ue_udp.dst_port;
71  }
72  else
73  {
74  clib_memcpy (&mp->udp_encap.src_ip.un.ip6,
75  &ue->ue_hdrs.ip6.ue_ip6.src_address, 16);
76  clib_memcpy (&mp->udp_encap.dst_ip.un.ip6,
77  &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16);
78  mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP6);
79  mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP6);
80 
81  /* ports aren't byte swapped because they are stored in network
82  * byte order */
83  mp->udp_encap.src_port = ue->ue_hdrs.ip6.ue_udp.src_port;
84  mp->udp_encap.dst_port = ue->ue_hdrs.ip6.ue_udp.dst_port;
85  }
86 
87  mp->udp_encap.table_id =
89  mp->udp_encap.id = htonl (ue - udp_encap_pool);
90 
91  vl_api_send_msg (reg, (u8 *) mp);
92 }
93 
94 static void
96  vlib_main_t * vm)
97 {
99  udp_encap_t *ue;
100 
102  if (!reg)
103  return;
104 
105  /* *INDENT-OFF* */
107  {
108  send_udp_encap_details(ue, reg, mp->context);
109  }
110  /* *INDENT-ON* */
111 }
112 
113 static void
115 {
117  ip46_address_t src_ip, dst_ip;
118  u32 fib_index, table_id;
119  fib_protocol_t fproto;
120  ip46_type_t itype;
121  index_t uei;
122  int rv = 0;
123 
124  uei = INDEX_INVALID;
125  table_id = ntohl (mp->udp_encap.table_id);
126 
127  itype = ip_address_decode (&mp->udp_encap.src_ip, &src_ip);
128  itype = ip_address_decode (&mp->udp_encap.dst_ip, &dst_ip);
129  fproto = fib_proto_from_ip46 (itype);
130  fib_index = fib_table_find (fproto, table_id);
131 
132  if (~0 == fib_index)
133  {
134  rv = VNET_API_ERROR_NO_SUCH_TABLE;
135  goto done;
136  }
137 
138  uei = udp_encap_add_and_lock (fproto, fib_index,
139  &src_ip, &dst_ip,
140  ntohs (mp->udp_encap.src_port),
141  ntohs (mp->udp_encap.dst_port),
143 
144 done:
145  /* *INDENT-OFF* */
146  REPLY_MACRO2 (VL_API_UDP_ENCAP_ADD_REPLY,
147  ({
148  rmp->id = ntohl (uei);
149  }));
150  /* *INDENT-ON* */
151 
152 }
153 
154 static void
156 {
157  vl_api_udp_encap_del_reply_t *rmp;
158  int rv = 0;
159 
160  udp_encap_unlock (ntohl (mp->id));
161 
162  REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY);
163 }
164 
165 #define vl_msg_name_crc_list
166 #include <vnet/udp/udp.api.h>
167 #undef vl_msg_name_crc_list
168 
169 static void
171 {
172 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
173  foreach_vl_msg_name_crc_udp;
174 #undef _
175 }
176 
177 static clib_error_t *
179 {
180  api_main_t *am = vlibapi_get_main ();
181 
182 #define _(N,n) \
183  vl_msg_api_set_handlers(VL_API_##N, #n, \
184  vl_api_##n##_t_handler, \
185  vl_noop_handler, \
186  vl_api_##n##_t_endian, \
187  vl_api_##n##_t_print, \
188  sizeof(vl_api_##n##_t), 1);
190 #undef _
191 
192  /*
193  * Set up the (msg_name, crc, message-id) table
194  */
196 
197  return 0;
198 }
199 
201 
202 /*
203  * fd.io coding-style-patch-verification: ON
204  *
205  * Local Variables:
206  * eval: (c-set-style "gnu")
207  * End:
208  */
fib_protocol_t ue_ip_proto
the protocol of the IP header imposed
Definition: udp_encap.h:83
#define foreach_udp_api_msg
Definition: udp_api.c:42
The UDP encap representation.
Definition: udp_encap.h:46
udp_encap_t * udp_encap_pool
Pool of encaps.
Definition: udp_encap.c:30
#define ntohs(x)
Definition: af_xdp.bpf.c:29
static clib_error_t * udp_api_hookup(vlib_main_t *vm)
Definition: udp_api.c:178
VLIB_API_INIT_FUNCTION(udp_api_hookup)
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
static void vl_api_udp_encap_dump_t_handler(vl_api_udp_encap_dump_t *mp, vlib_main_t *vm)
Definition: udp_api.c:95
index_t udp_encap_add_and_lock(fib_protocol_t proto, index_t fib_index, const ip46_address_t *src_ip, const ip46_address_t *dst_ip, u16 src_port, u16 dst_port, udp_encap_fixup_flags_t flags)
Definition: udp_encap.c:56
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
vlib_main_t * vm
Definition: in2out_ed.c:1580
static void vl_api_udp_encap_add_t_handler(vl_api_udp_encap_add_t *mp, vlib_main_t *vm)
Definition: udp_api.c:114
void * vl_msg_api_alloc(int nbytes)
union udp_encap_t_::@558 ue_hdrs
The headers to paint, in packet painting order.
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void send_udp_encap_details(const udp_encap_t *ue, vl_api_registration_t *reg, u32 context)
Definition: udp_api.c:48
unsigned int u32
Definition: types.h:88
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1106
vl_api_udp_encap_t udp_encap
Definition: udp.api:58
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:186
fib_protocol_t fib_proto_from_ip46(ip46_type_t iproto)
Convert from ip46_type to fib_protocol.
Definition: fib_types.c:409
static void vl_api_udp_encap_del_t_handler(vl_api_udp_encap_del_t *mp, vlib_main_t *vm)
Definition: udp_api.c:155
struct udp_encap_t_::@558::@559 ip4
#define REPLY_MACRO(t)
Del UDP encap.
Definition: udp.api:81
struct udp_encap_t_::@558::@560 ip6
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
index_t ue_fib_index
The FIB index in which the encap destination resides.
Definition: udp_encap.h:104
UDP encap details during dump.
Definition: udp.api:100
vl_api_address_family_t ip_address_family_encode(ip_address_family_t af)
Definition: ip_types_api.c:50
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
static void setup_message_id_table(api_main_t *am)
Definition: udp_api.c:170
Add UDP encap.
Definition: udp.api:54
u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto)
Get the Table-ID of the FIB from protocol and index.
Definition: fib_table.c:1095
dump UDP encaps
Definition: udp.api:91
Add UDP encap reply.
Definition: udp.api:68
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
vl_api_address_t src_ip
Definition: wireguard.api:38
u32 table_id
Definition: wireguard.api:102
vl_api_address_t dst_ip
Definition: udp.api:44
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
void udp_encap_unlock(index_t uei)
Definition: udp_encap.c:162
vl_api_udp_encap_t udp_encap
Definition: udp.api:103
ip46_type_t
Definition: ip46_address.h:22