session: avoid session handle conflict with vcl
[vpp.git] / src / vnet / session / application_interface.c
1 /*
2  * Copyright (c) 2016 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 #include <vnet/session/application_interface.h>
16
17 #include <vnet/session/session.h>
18 #include <vlibmemory/api.h>
19 #include <vnet/dpo/load_balance.h>
20
21 /** @file
22     VPP's application/session API bind/unbind/connect/disconnect calls
23 */
24
25 static u8
26 session_endpoint_is_local (session_endpoint_t * sep)
27 {
28   return (ip_is_zero (&sep->ip, sep->is_ip4)
29           || ip_is_local_host (&sep->ip, sep->is_ip4));
30 }
31
32 static u8
33 session_endpoint_is_zero (session_endpoint_t * sep)
34 {
35   return ip_is_zero (&sep->ip, sep->is_ip4);
36 }
37
38 u8
39 session_endpoint_in_ns (session_endpoint_t * sep)
40 {
41   u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4);
42   if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX
43       && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
44     {
45       clib_warning ("sw_if_index %u not configured with ip %U",
46                     sep->sw_if_index, format_ip46_address, &sep->ip,
47                     sep->is_ip4);
48       return 0;
49     }
50   return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
51 }
52
53 int
54 api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
55 {
56   session_manager_main_t *smm = vnet_get_session_manager_main ();
57   stream_session_t *pool;
58
59   *thread_index = handle & 0xFFFFFFFF;
60   *session_index = handle >> 32;
61
62   if (*thread_index >= vec_len (smm->sessions))
63     return VNET_API_ERROR_INVALID_VALUE;
64
65   pool = smm->sessions[*thread_index];
66
67   if (pool_is_free_index (pool, *session_index))
68     return VNET_API_ERROR_INVALID_VALUE_2;
69
70   return 0;
71 }
72
73 static void
74 session_endpoint_update_for_app (session_endpoint_t * sep,
75                                  application_t * app)
76 {
77   app_namespace_t *app_ns;
78   app_ns = app_namespace_get (app->ns_index);
79   if (app_ns)
80     {
81       /* Ask transport and network to bind to/connect using local interface
82        * that "supports" app's namespace. This will fix our local connection
83        * endpoint.
84        */
85       sep->sw_if_index = app_ns->sw_if_index;
86       sep->fib_index =
87         sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
88     }
89 }
90
91 static int
92 vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
93 {
94   u64 lh, ll_handle = SESSION_INVALID_HANDLE;
95   application_t *app;
96   u32 table_index;
97   int rv;
98
99   app = application_get_if_valid (app_index);
100   if (!app)
101     {
102       SESSION_DBG ("app not attached");
103       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
104     }
105
106   session_endpoint_update_for_app (sep, app);
107   if (!session_endpoint_in_ns (sep))
108     return VNET_API_ERROR_INVALID_VALUE_2;
109
110   table_index = application_session_table (app,
111                                            session_endpoint_fib_proto (sep));
112   lh = session_lookup_endpoint_listener (table_index, sep, 1);
113   if (lh != SESSION_INVALID_HANDLE)
114     return VNET_API_ERROR_ADDRESS_IN_USE;
115
116   /*
117    * Add session endpoint to local session table. Only binds to "inaddr_any"
118    * (i.e., zero address) are added to local scope table.
119    */
120   if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
121     {
122       if ((rv = application_start_local_listen (app, sep, handle)))
123         return rv;
124       ll_handle = *handle;
125     }
126
127   if (!application_has_global_scope (app))
128     return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
129
130   /*
131    * Add session endpoint to global session table
132    */
133
134   /* Setup listen path down to transport */
135   rv = application_start_listen (app, sep, handle);
136   if (rv && ll_handle != SESSION_INVALID_HANDLE)
137     session_lookup_del_session_endpoint (table_index, sep);
138
139   /*
140    * Store in local table listener the index of the transport layer
141    * listener. We'll need local listeners are hit and we need to
142    * return global handle
143    */
144   if (ll_handle != SESSION_INVALID_HANDLE)
145     {
146       local_session_t *ll;
147       stream_session_t *tl;
148       ll = application_get_local_listener_w_handle (ll_handle);
149       tl = listen_session_get_from_handle (*handle);
150       ll->transport_listener_index = tl->session_index;
151     }
152   return rv;
153 }
154
155 int
156 vnet_unbind_i (u32 app_index, session_handle_t handle)
157 {
158   application_t *app;
159   int rv;
160
161   if (!(app = application_get_if_valid (app_index)))
162     {
163       SESSION_DBG ("app (%d) not attached", app_index);
164       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
165     }
166
167   if (application_has_local_scope (app))
168     {
169       if ((rv = application_stop_local_listen (app, handle)))
170         return rv;
171     }
172
173   /*
174    * Clear the global scope table of the listener
175    */
176   if (application_has_global_scope (app))
177     return application_stop_listen (app, handle);
178   return 0;
179 }
180
181 int
182 vnet_connect_i (u32 client_index, u32 api_context, session_endpoint_t * sep,
183                 void *mp)
184 {
185   application_t *server, *client;
186   u32 table_index, server_index, li;
187   stream_session_t *listener;
188   local_session_t *ll;
189   u64 lh;
190
191   if (session_endpoint_is_zero (sep))
192     return VNET_API_ERROR_INVALID_VALUE;
193
194   client = application_get (client_index);
195   session_endpoint_update_for_app (sep, client);
196
197   /*
198    * First check the local scope for locally attached destinations.
199    * If we have local scope, we pass *all* connects through it since we may
200    * have special policy rules even for non-local destinations, think proxy.
201    */
202   if (application_has_local_scope (client))
203     {
204       table_index = application_local_session_table (client);
205       lh = session_lookup_local_endpoint (table_index, sep);
206       if (lh == SESSION_DROP_HANDLE)
207         return VNET_API_ERROR_APP_CONNECT_FILTERED;
208
209       if (lh == SESSION_INVALID_HANDLE)
210         goto global_scope;
211
212       local_session_parse_handle (lh, &server_index, &li);
213
214       /*
215        * Break loop if rule in local table points to connecting app. This
216        * can happen if client is a generic proxy. Route connect through
217        * global table instead.
218        */
219       if (server_index != client_index)
220         {
221           server = application_get (server_index);
222           ll = application_get_local_listen_session (server, li);
223           return application_local_session_connect (table_index, client,
224                                                     server, ll, api_context);
225         }
226     }
227
228   /*
229    * If nothing found, check the global scope for locally attached
230    * destinations. Make sure first that we're allowed to.
231    */
232
233 global_scope:
234   if (session_endpoint_is_local (sep))
235     return VNET_API_ERROR_SESSION_CONNECT;
236
237   if (!application_has_global_scope (client))
238     return VNET_API_ERROR_APP_CONNECT_SCOPE;
239
240   table_index = application_session_table (client,
241                                            session_endpoint_fib_proto (sep));
242   listener = session_lookup_listener (table_index, sep);
243   if (listener)
244     {
245       server = application_get (listener->app_index);
246       if (server)
247         return application_local_session_connect (table_index, client, server,
248                                                   (local_session_t *)
249                                                   listener, api_context);
250     }
251
252   /*
253    * Not connecting to a local server, propagate to transport
254    */
255   if (application_open_session (client, sep, api_context))
256     return VNET_API_ERROR_SESSION_CONNECT;
257   return 0;
258 }
259
260 /**
261  * unformat a vnet URI
262  *
263  * transport-proto://ip46-addr:port
264  * eg. tcp://ip46-addr:port
265  *
266  * u8 ip46_address[16];
267  * u16  port_in_host_byte_order;
268  * stream_session_type_t sst;
269  * u8 *fifo_name;
270  *
271  * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
272  *              &sst, &port, &fifo_name))
273  *  etc...
274  *
275  */
276 uword
277 unformat_vnet_uri (unformat_input_t * input, va_list * args)
278 {
279   session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
280   u32 transport_proto = 0;
281   if (unformat (input, "%U://%U/%d", unformat_transport_proto,
282                 &transport_proto, unformat_ip4_address, &sep->ip.ip4,
283                 &sep->port))
284     {
285       sep->transport_proto = transport_proto;
286       sep->port = clib_host_to_net_u16 (sep->port);
287       sep->is_ip4 = 1;
288       return 1;
289     }
290   if (unformat (input, "%U://%U/%d", unformat_transport_proto,
291                 &transport_proto, unformat_ip6_address, &sep->ip.ip6,
292                 &sep->port))
293     {
294       sep->transport_proto = transport_proto;
295       sep->port = clib_host_to_net_u16 (sep->port);
296       sep->is_ip4 = 0;
297       return 1;
298     }
299   return 0;
300 }
301
302 static u8 *cache_uri;
303 static session_endpoint_t *cache_sep;
304
305 int
306 parse_uri (char *uri, session_endpoint_t * sep)
307 {
308   unformat_input_t _input, *input = &_input;
309
310   if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
311     {
312       *sep = *cache_sep;
313       return 0;
314     }
315
316   /* Make sure */
317   uri = (char *) format (0, "%s%c", uri, 0);
318
319   /* Parse uri */
320   unformat_init_string (input, uri, strlen (uri));
321   if (!unformat (input, "%U", unformat_vnet_uri, sep))
322     {
323       unformat_free (input);
324       return VNET_API_ERROR_INVALID_VALUE;
325     }
326   unformat_free (input);
327
328   vec_free (cache_uri);
329   cache_uri = (u8 *) uri;
330   if (cache_sep)
331     clib_mem_free (cache_sep);
332   cache_sep = clib_mem_alloc (sizeof (*sep));
333   *cache_sep = *sep;
334
335   return 0;
336 }
337
338 static int
339 session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
340 {
341   app_namespace_t *app_ns;
342   if (vec_len (namespace_id) == 0)
343     {
344       /* Use default namespace */
345       *app_ns_index = 0;
346       return 0;
347     }
348
349   *app_ns_index = app_namespace_index_from_id (namespace_id);
350   if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
351     return VNET_API_ERROR_APP_INVALID_NS;
352   app_ns = app_namespace_get (*app_ns_index);
353   if (!app_ns)
354     return VNET_API_ERROR_APP_INVALID_NS;
355   if (app_ns->ns_secret != secret)
356     return VNET_API_ERROR_APP_WRONG_NS_SECRET;
357   return 0;
358 }
359
360 /**
361  * Attach application to vpp
362  *
363  * Allocates a vpp app, i.e., a structure that keeps back pointers
364  * to external app and a segment manager for shared memory fifo based
365  * communication with the external app.
366  */
367 clib_error_t *
368 vnet_application_attach (vnet_app_attach_args_t * a)
369 {
370   svm_fifo_segment_private_t *fs;
371   application_t *app = 0;
372   segment_manager_t *sm;
373   u32 app_ns_index = 0;
374   u64 secret;
375   int rv;
376
377   app = application_lookup (a->api_client_index);
378   if (app)
379     return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
380                                    0, "app already attached");
381
382   secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
383   if ((rv = session_validate_namespace (a->namespace_id, secret,
384                                         &app_ns_index)))
385     return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
386   a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
387   app = application_new ();
388   if ((rv = application_init (app, a->api_client_index, a->options,
389                               a->session_cb_vft)))
390     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
391
392   a->app_event_queue_address = pointer_to_uword (app->event_queue);
393   sm = segment_manager_get (app->first_segment_manager);
394   fs = segment_manager_get_segment_w_lock (sm, 0);
395
396   if (application_is_proxy (app))
397     application_setup_proxy (app);
398
399   ASSERT (vec_len (fs->ssvm.name) <= 128);
400   a->segment = &fs->ssvm;
401   a->app_index = app->index;
402
403   segment_manager_segment_reader_unlock (sm);
404
405   return 0;
406 }
407
408 /**
409  * Detach application from vpp
410  */
411 int
412 vnet_application_detach (vnet_app_detach_args_t * a)
413 {
414   application_t *app;
415   app = application_get_if_valid (a->app_index);
416
417   if (!app)
418     {
419       clib_warning ("app not attached");
420       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
421     }
422
423   application_del (app);
424   return 0;
425 }
426
427 int
428 vnet_bind_uri (vnet_bind_args_t * a)
429 {
430   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
431   int rv;
432
433   rv = parse_uri (a->uri, &sep);
434   if (rv)
435     return rv;
436
437   return vnet_bind_i (a->app_index, &sep, &a->handle);
438 }
439
440 int
441 vnet_unbind_uri (vnet_unbind_args_t * a)
442 {
443   stream_session_t *listener;
444   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
445   int rv;
446
447   rv = parse_uri (a->uri, &sep);
448   if (rv)
449     return rv;
450
451   /* NOTE: only default table supported for uri */
452   listener = session_lookup_listener (0, &sep);
453   if (!listener)
454     return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
455
456   return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
457 }
458
459 clib_error_t *
460 vnet_connect_uri (vnet_connect_args_t * a)
461 {
462   session_endpoint_t sep_null = SESSION_ENDPOINT_NULL;
463   int rv;
464
465   /* Parse uri */
466   a->sep = sep_null;
467   rv = parse_uri (a->uri, &a->sep);
468   if (rv)
469     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
470   if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
471     return clib_error_return_code (0, rv, 0, "connect failed");
472   return 0;
473 }
474
475 int
476 vnet_disconnect_session (vnet_disconnect_args_t * a)
477 {
478   if (session_handle_is_local (a->handle))
479     {
480       local_session_t *ls;
481       ls = application_get_local_session_from_handle (a->handle);
482       if (ls->app_index != a->app_index && ls->client_index != a->app_index)
483         {
484           clib_warning ("app %u is neither client nor server for session %u",
485                         a->app_index, a->app_index);
486           return VNET_API_ERROR_INVALID_VALUE;
487         }
488       return application_local_session_disconnect (a->app_index, ls);
489     }
490   else
491     {
492       stream_session_t *s;
493       s = session_get_from_handle_if_valid (a->handle);
494       if (!s || s->app_index != a->app_index)
495         return VNET_API_ERROR_INVALID_VALUE;
496
497       /* We're peeking into another's thread pool. Make sure */
498       ASSERT (s->session_index == session_index_from_handle (a->handle));
499
500       stream_session_disconnect (s);
501     }
502   return 0;
503 }
504
505 clib_error_t *
506 vnet_bind (vnet_bind_args_t * a)
507 {
508   int rv;
509   if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
510     return clib_error_return_code (0, rv, 0, "bind failed");
511   return 0;
512 }
513
514 clib_error_t *
515 vnet_unbind (vnet_unbind_args_t * a)
516 {
517   int rv;
518   if ((rv = vnet_unbind_i (a->app_index, a->handle)))
519     return clib_error_return_code (0, rv, 0, "unbind failed");
520   return 0;
521 }
522
523 clib_error_t *
524 vnet_connect (vnet_connect_args_t * a)
525 {
526   int rv;
527   if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
528     return clib_error_return_code (0, rv, 0, "connect failed");
529   return 0;
530 }
531
532 /*
533  * fd.io coding-style-patch-verification: ON
534  *
535  * Local Variables:
536  * eval: (c-set-style "gnu")
537  * End:
538  */