FD.io VPP  v21.10.1-2-g0a485f517
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_local.h>
20 #include <vnet/udp/udp_encap.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <vnet/udp/udp.h>
24 
25 #include <vnet/format_fns.h>
26 #include <vnet/udp/udp.api_enum.h>
27 #include <vnet/udp/udp.api_types.h>
28 
29 #define REPLY_MSG_ID_BASE udp_main.msg_id_base
31 
32 static void
34  u32 context)
35 {
37 
38  mp = vl_msg_api_alloc (sizeof (*mp));
39  clib_memset (mp, 0, sizeof (*mp));
40  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DETAILS);
41  mp->context = context;
42 
43  if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
44  {
45  clib_memcpy (&mp->udp_encap.src_ip.un.ip4,
46  &ue->ue_hdrs.ip4.ue_ip4.src_address, 4);
47  clib_memcpy (&mp->udp_encap.dst_ip.un.ip4,
48  &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4);
49  mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP4);
50  mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP4);
51 
52  /* ports aren't byte swapped because they are stored in network
53  * byte order */
54  mp->udp_encap.src_port = ue->ue_hdrs.ip4.ue_udp.src_port;
55  mp->udp_encap.dst_port = ue->ue_hdrs.ip4.ue_udp.dst_port;
56  }
57  else
58  {
59  clib_memcpy (&mp->udp_encap.src_ip.un.ip6,
60  &ue->ue_hdrs.ip6.ue_ip6.src_address, 16);
61  clib_memcpy (&mp->udp_encap.dst_ip.un.ip6,
62  &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16);
63  mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP6);
64  mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP6);
65 
66  /* ports aren't byte swapped because they are stored in network
67  * byte order */
68  mp->udp_encap.src_port = ue->ue_hdrs.ip6.ue_udp.src_port;
69  mp->udp_encap.dst_port = ue->ue_hdrs.ip6.ue_udp.dst_port;
70  }
71 
72  mp->udp_encap.table_id =
74  mp->udp_encap.id = htonl (ue - udp_encap_pool);
75 
76  vl_api_send_msg (reg, (u8 *) mp);
77 }
78 
79 static void
81 {
83  udp_encap_t *ue;
84 
86  if (!reg)
87  return;
88 
89  /* *INDENT-OFF* */
91  {
92  send_udp_encap_details(ue, reg, mp->context);
93  }
94  /* *INDENT-ON* */
95 }
96 
97 static void
99 {
101  ip46_address_t src_ip, dst_ip;
102  u32 fib_index, table_id;
103  fib_protocol_t fproto;
104  ip46_type_t itype;
105  index_t uei;
106  int rv = 0;
107 
108  uei = INDEX_INVALID;
109  table_id = ntohl (mp->udp_encap.table_id);
110 
111  itype = ip_address_decode (&mp->udp_encap.src_ip, &src_ip);
112  itype = ip_address_decode (&mp->udp_encap.dst_ip, &dst_ip);
113  fproto = fib_proto_from_ip46 (itype);
114  fib_index = fib_table_find (fproto, table_id);
115 
116  if (~0 == fib_index)
117  {
118  rv = VNET_API_ERROR_NO_SUCH_TABLE;
119  goto done;
120  }
121 
122  uei = udp_encap_add_and_lock (fproto, fib_index,
123  &src_ip, &dst_ip,
124  ntohs (mp->udp_encap.src_port),
125  ntohs (mp->udp_encap.dst_port),
127 
128 done:
129  /* *INDENT-OFF* */
130  REPLY_MACRO2 (VL_API_UDP_ENCAP_ADD_REPLY,
131  ({
132  rmp->id = ntohl (uei);
133  }));
134  /* *INDENT-ON* */
135 
136 }
137 
138 static void
140 {
141  vl_api_udp_encap_del_reply_t *rmp;
142  int rv = 0;
143 
144  udp_encap_unlock (ntohl (mp->id));
145 
146  REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY);
147 }
148 
149 u32
151  vl_api_udp_decap_next_proto_t iproto)
152 {
153  switch (iproto)
154  {
156  return vlib_get_node_by_name (vm, (u8 *) "ip4-input")->index;
158  return vlib_get_node_by_name (vm, (u8 *) "ip6-input")->index;
160  return vlib_get_node_by_name (vm, (u8 *) "mpls-input")->index;
161  }
162  return ~0;
163 }
164 
165 static void
167 {
168  vl_api_udp_decap_add_del_reply_t *rmp;
170  int rv = 0;
171 
172  if (mp->is_add)
173  {
174  u32 node_index =
175  udp_api_decap_proto_to_index (vm, ntohl (mp->udp_decap.next_proto));
176  if (node_index == ~0)
177  rv = VNET_API_ERROR_INVALID_PROTOCOL;
178  else
180  mp->udp_decap.is_ip4);
181  }
182  else
184  mp->udp_decap.is_ip4);
185  REPLY_MACRO (VL_API_UDP_DECAP_ADD_DEL_REPLY);
186 }
187 
188 #include <vnet/udp/udp.api.c>
189 static clib_error_t *
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_proto_from_ip46
fib_protocol_t fib_proto_from_ip46(ip46_type_t iproto)
Convert from ip46_type to fib_protocol.
Definition: fib_types.c:409
UDP_API_DECAP_PROTO_IP4
@ UDP_API_DECAP_PROTO_IP4
Definition: udp.api:50
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(udp_api_hookup)
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
fib_table_get_table_id
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:1100
udp_encap_t_::ue_fib_index
index_t ue_fib_index
The FIB index in which the encap destination resides.
Definition: udp_encap.h:104
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_udp_encap_details_t
UDP encap details during dump.
Definition: udp.api:120
udp_encap_pool
udp_encap_t * udp_encap_pool
Pool of encaps.
Definition: udp_encap.c:30
fib_table.h
vl_api_udp_decap_add_del_t_handler
static void vl_api_udp_decap_add_del_t_handler(vl_api_udp_decap_add_del_t *mp)
Definition: udp_api.c:166
udp_encap_t_::ip4
struct udp_encap_t_::@565::@566 ip4
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
AF_IP4
@ AF_IP4
Definition: ip_types.h:23
udp_encap_t_::ip6
struct udp_encap_t_::@565::@567 ip6
node_index
node node_index
Definition: interface_output.c:440
vl_api_udp_encap_details_t::udp_encap
vl_api_udp_encap_t udp_encap
Definition: udp.api:123
udp_encap_t_
The UDP encap representation.
Definition: udp_encap.h:46
ip_address_decode
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:172
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vlib_node_t::index
u32 index
Definition: node.h:269
udp_encap.h
vl_api_udp_encap_del_t_handler
static void vl_api_udp_encap_del_t_handler(vl_api_udp_encap_del_t *mp)
Definition: udp_api.c:139
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
dst_ip
vl_api_address_t dst_ip
Definition: udp.api:44
index_t
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:43
vl_api_udp_encap_add_reply_t
Add UDP encap reply.
Definition: udp.api:88
UDP_API_DECAP_PROTO_MPLS
@ UDP_API_DECAP_PROTO_MPLS
Definition: udp.api:52
udp_local.h
udp_register_dst_port
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:431
UDP_ENCAP_FIXUP_NONE
@ UDP_ENCAP_FIXUP_NONE
Definition: udp_encap.h:36
udp_encap_unlock
void udp_encap_unlock(index_t uei)
Definition: udp_encap.c:162
vl_api_udp_encap_add_t
Add UDP encap.
Definition: udp.api:74
udp_unregister_dst_port
void udp_unregister_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp_local.c:469
vl_api_udp_encap_del_t
Del UDP encap.
Definition: udp.api:101
REPLY_MSG_ID_BASE
#define REPLY_MSG_ID_BASE
Definition: udp_api.c:29
fib_protocol_t
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
send_udp_encap_details
static void send_udp_encap_details(const udp_encap_t *ue, vl_api_registration_t *reg, u32 context)
Definition: udp_api.c:33
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
udp_encap_t_::ue_ip_proto
fib_protocol_t ue_ip_proto
the protocol of the IP header imposed
Definition: udp_encap.h:83
ip_address_family_encode
vl_api_address_family_t ip_address_family_encode(ip_address_family_t af)
Definition: ip_types_api.c:36
udp_api_hookup
static clib_error_t * udp_api_hookup(vlib_main_t *vm)
Definition: udp_api.c:190
udp_api_decap_proto_to_index
u32 udp_api_decap_proto_to_index(vlib_main_t *vm, vl_api_udp_decap_next_proto_t iproto)
Definition: udp_api.c:150
vl_api_udp_encap_dump_t::context
u32 context
Definition: udp.api:114
vl_api_udp_encap_add_t_handler
static void vl_api_udp_encap_add_t_handler(vl_api_udp_encap_add_t *mp)
Definition: udp_api.c:98
vlib_get_node_by_name
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
vl_api_udp_encap_del_t::id
u32 id
Definition: udp.api:105
udp_encap_add_and_lock
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
format_fns.h
u32
unsigned int u32
Definition: types.h:88
vl_api_udp_encap_add_reply_t::id
u32 id
Definition: udp.api:92
table_id
u32 table_id
Definition: wireguard.api:102
vl_api_udp_encap_add_t::udp_encap
vl_api_udp_encap_t udp_encap
Definition: udp.api:78
api_helper_macros.h
AF_IP6
@ AF_IP6
Definition: ip_types.h:24
udp.h
src_ip
vl_api_address_t src_ip
Definition: wireguard.api:38
udp_encap_t_::ue_hdrs
union udp_encap_t_::@565 ue_hdrs
The headers to paint, in packet painting order.
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vl_api_udp_encap_dump_t
dump UDP encaps
Definition: udp.api:111
ip46_type_t
ip46_type_t
Definition: ip46_address.h:22
UDP_API_DECAP_PROTO_IP6
@ UDP_API_DECAP_PROTO_IP6
Definition: udp.api:51
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
vl_api_udp_decap_add_del_t::udp_decap
vl_api_udp_decap_t udp_decap
Definition: udp.api:138
INDEX_INVALID
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:49
fib_table_find
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:1111
ip_types_api.h
vl_api_udp_encap_dump_t_handler
static void vl_api_udp_encap_dump_t_handler(vl_api_udp_encap_dump_t *mp)
Definition: udp_api.c:80
vl_api_udp_encap_details_t::context
u32 context
Definition: udp.api:122
vl_api_udp_decap_add_del_t
Add/Del UDP decap.
Definition: udp.api:133
vl_api_udp_encap_dump_t::client_index
u32 client_index
Definition: udp.api:113
vl_api_udp_decap_add_del_t::is_add
bool is_add
Definition: udp.api:137
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199