FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
application_interface.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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  */
17 #include <vnet/session/session.h>
18 
19 /** @file
20  VPP's application/session API bind/unbind/connect/disconnect calls
21 */
22 
23 /**
24  * unformat a vnet URI
25  *
26  * transport-proto://[hostname]ip46-addr:port
27  * eg. tcp://ip46-addr:port
28  * tls://[testtsl.fd.io]ip46-addr:port
29  *
30  * u8 ip46_address[16];
31  * u16 port_in_host_byte_order;
32  * stream_session_type_t sst;
33  * u8 *fifo_name;
34  *
35  * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
36  * &sst, &port, &fifo_name))
37  * etc...
38  *
39  */
40 uword
41 unformat_vnet_uri (unformat_input_t * input, va_list * args)
42 {
43  session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *);
45 
46  if (unformat (input, "%U://%U/%d", unformat_transport_proto,
47  &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port))
48  {
49  sep->transport_proto = transport_proto;
50  sep->port = clib_host_to_net_u16 (port);
51  sep->is_ip4 = 1;
52  return 1;
53  }
54  else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
55  &transport_proto, unformat_ip6_address, &sep->ip.ip6,
56  &port))
57  {
58  sep->transport_proto = transport_proto;
59  sep->port = clib_host_to_net_u16 (port);
60  sep->is_ip4 = 0;
61  return 1;
62  }
63  else if (unformat (input, "%U://session/%lu", unformat_transport_proto,
64  &transport_proto, &sep->parent_handle))
65  {
66  sep->transport_proto = transport_proto;
67  sep->ip.ip4.as_u32 = 1; /* ip need to be non zero in vnet */
68  return 1;
69  }
70  return 0;
71 }
72 
73 static u8 *cache_uri;
75 
76 int
78 {
79  unformat_input_t _input, *input = &_input;
80 
81  if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
82  {
83  *sep = *cache_sep;
84  return 0;
85  }
86 
87  /* Make sure */
88  uri = (char *) format (0, "%s%c", uri, 0);
89 
90  /* Parse uri */
91  unformat_init_string (input, uri, strlen (uri));
92  if (!unformat (input, "%U", unformat_vnet_uri, sep))
93  {
94  unformat_free (input);
95  return VNET_API_ERROR_INVALID_VALUE;
96  }
97  unformat_free (input);
98 
100  cache_uri = (u8 *) uri;
101  if (cache_sep)
103  cache_sep = clib_mem_alloc (sizeof (*sep));
104  *cache_sep = *sep;
105 
106  return 0;
107 }
108 
109 int
111 {
113  int rv;
114 
115  rv = parse_uri (a->uri, &sep);
116  if (rv)
117  return rv;
118  sep.app_wrk_index = 0;
119  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
120  return vnet_listen (a);
121 }
122 
123 int
125 {
127  application_t *app;
129  u32 table_index;
130  int rv;
131 
132  if ((rv = parse_uri (a->uri, &sep)))
133  return rv;
134 
135  app = application_get (a->app_index);
136  if (!app)
137  return VNET_API_ERROR_INVALID_VALUE;
138 
139  table_index = application_session_table (app, fib_ip_proto (!sep.is_ip4));
140  listener = session_lookup_listener (table_index,
141  (session_endpoint_t *) & sep);
142  if (!listener)
143  return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
145  return vnet_unlisten (a);
146 }
147 
148 int
150 {
152  int rv;
153 
154  if ((rv = parse_uri (a->uri, &sep)))
155  return rv;
156 
157  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
158  if ((rv = vnet_connect (a)))
159  return rv;
160  return 0;
161 }
162 
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "gnu")
168  * End:
169  */
vnet_listen
int vnet_listen(vnet_listen_args_t *a)
Definition: application.c:1266
vnet_connect
int vnet_connect(vnet_connect_args_t *a)
Definition: application.c:1320
cache_sep
static session_endpoint_cfg_t * cache_sep
Definition: application_interface.c:74
fib_ip_proto
static fib_protocol_t fib_ip_proto(bool is_ip6)
Convert from boolean is_ip6 to FIB protocol.
Definition: fib_types.h:79
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
vnet_unlisten
int vnet_unlisten(vnet_unlisten_args_t *a)
Definition: application.c:1359
listen_session_get_handle
static u64 listen_session_get_handle(session_t *s)
Definition: session.h:622
clib_mem_free
static void clib_mem_free(void *p)
Definition: mem.h:311
session_
Definition: session_types.h:175
session_endpoint_cfg_t
struct _session_endpoint_cfg session_endpoint_cfg_t
vnet_connect_uri
int vnet_connect_uri(vnet_connect_args_t *a)
Definition: application_interface.c:149
listener
Definition: test_stats.cpp:7
port
u16 port
Definition: lb_types.api:73
vnet_unlisten_args_t
struct _vnet_unlisten_args_t vnet_unlisten_args_t
application_
Definition: application.h:108
unformat_input_t
struct _unformat_input_t unformat_input_t
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
SESSION_ENDPOINT_CFG_NULL
#define SESSION_ENDPOINT_CFG_NULL
Definition: session_types.h:77
application_get
application_t * application_get(u32 app_index)
Definition: application.c:710
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
unformat_transport_proto
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:155
application_session_table
u32 application_session_table(application_t *app, u8 fib_proto)
Definition: application.c:358
vnet_listen_args_t
struct _vnet_bind_args_t vnet_listen_args_t
cache_uri
static u8 * cache_uri
Definition: application_interface.c:73
uword
u64 uword
Definition: types.h:112
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
unformat_init_string
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
format
description fragment has unexpected format
Definition: map.api:433
u32
unsigned int u32
Definition: types.h:88
session_endpoint_t
struct _session_endpoint session_endpoint_t
vnet_connect_args_t
struct _vnet_connect_args vnet_connect_args_t
session_lookup_listener
session_t * session_lookup_listener(u32 table_index, session_endpoint_t *sep)
Lookup listener, exact or proxy (inaddr_any:0) match.
Definition: session_lookup.c:742
parse_uri
int parse_uri(char *uri, session_endpoint_cfg_t *sep)
Definition: application_interface.c:77
application_interface.h
vnet_bind_uri
int vnet_bind_uri(vnet_listen_args_t *a)
Definition: application_interface.c:110
u8
unsigned char u8
Definition: types.h:56
vnet_unbind_uri
int vnet_unbind_uri(vnet_unlisten_args_t *a)
Definition: application_interface.c:124
a
a
Definition: bitmap.h:544
session.h
unformat_ip6_address
unformat_function_t unformat_ip6_address
Definition: format.h:89
rv
int __clib_unused rv
Definition: application.c:491
unformat_ip4_address
unformat_function_t unformat_ip4_address
Definition: format.h:68
transport_proto
transport_proto
Definition: session.api:22
unformat_vnet_uri
uword unformat_vnet_uri(unformat_input_t *input, va_list *args)
unformat a vnet URI
Definition: application_interface.c:41
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
application.h