session: add support for proxying apps
[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   application_t *app;
95   u32 table_index;
96   u64 listener;
97   int rv, have_local = 0;
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   listener = session_lookup_session_endpoint (table_index, sep);
113   if (listener != 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       table_index = application_local_session_table (app);
123       listener = session_lookup_session_endpoint (table_index, sep);
124       if (listener != SESSION_INVALID_HANDLE)
125         return VNET_API_ERROR_ADDRESS_IN_USE;
126       session_lookup_add_session_endpoint (table_index, sep, app->index);
127       *handle = session_lookup_local_listener_make_handle (sep);
128       have_local = 1;
129     }
130
131   if (!application_has_global_scope (app))
132     return (have_local - 1);
133
134   /*
135    * Add session endpoint to global session table
136    */
137
138   /* Setup listen path down to transport */
139   rv = application_start_listen (app, sep, handle);
140   if (rv && have_local)
141     session_lookup_del_session_endpoint (table_index, sep);
142   return rv;
143 }
144
145 int
146 vnet_unbind_i (u32 app_index, u64 handle)
147 {
148   application_t *app = application_get_if_valid (app_index);
149   stream_session_t *listener = 0;
150   u32 table_index;
151
152   if (!app)
153     {
154       SESSION_DBG ("app (%d) not attached", app_index);
155       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
156     }
157
158   /*
159    * Clean up local session table. If we have a listener session use it to
160    * find the port and proto. If not, the handle must be a local table handle
161    * so parse it.
162    */
163
164   if (application_has_local_scope (app))
165     {
166       session_endpoint_t sep = SESSION_ENDPOINT_NULL;
167       if (!session_lookup_local_is_handle (handle))
168         listener = listen_session_get_from_handle (handle);
169       if (listener)
170         {
171           if (listen_session_get_local_session_endpoint (listener, &sep))
172             {
173               clib_warning ("broken listener");
174               return -1;
175             }
176         }
177       else
178         {
179           if (session_lookup_local_listener_parse_handle (handle, &sep))
180             {
181               clib_warning ("can't parse handle");
182               return -1;
183             }
184         }
185       table_index = application_local_session_table (app);
186       session_lookup_del_session_endpoint (table_index, &sep);
187     }
188
189   /*
190    * Clear the global scope table of the listener
191    */
192   if (application_has_global_scope (app))
193     return application_stop_listen (app, handle);
194   return 0;
195 }
196
197 static int
198 app_connect_redirect (application_t * server, void *mp)
199 {
200   return server->cb_fns.redirect_connect_callback (server->api_client_index,
201                                                    mp);
202 }
203
204 int
205 vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
206                 void *mp)
207 {
208   application_t *server, *app;
209   u32 table_index, server_index;
210   stream_session_t *listener;
211
212   if (session_endpoint_is_zero (sep))
213     return VNET_API_ERROR_INVALID_VALUE;
214
215   app = application_get (app_index);
216   session_endpoint_update_for_app (sep, app);
217
218   /*
219    * First check the the local scope for locally attached destinations.
220    * If we have local scope, we pass *all* connects through it since we may
221    * have special policy rules even for non-local destinations, think proxy.
222    */
223   if (application_has_local_scope (app))
224     {
225       table_index = application_local_session_table (app);
226       server_index = session_lookup_local_session_endpoint (table_index, sep);
227
228       /*
229        * Break loop if rule in local table points to connecting app. This
230        * can happen if client is a generic proxy. Route connect through
231        * global table instead.
232        */
233       if (server_index != app_index)
234         {
235           server = application_get (server_index);
236           /*
237            * Server is willing to have a direct fifo connection created
238            * instead of going through the state machine, etc.
239            */
240           if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
241             return app_connect_redirect (server, mp);
242         }
243     }
244
245   /*
246    * If nothing found, check the global scope for locally attached
247    * destinations. Make sure first that we're allowed to.
248    */
249   if (session_endpoint_is_local (sep))
250     return VNET_API_ERROR_SESSION_CONNECT;
251
252   if (!application_has_global_scope (app))
253     return VNET_API_ERROR_APP_CONNECT_SCOPE;
254
255   table_index = application_session_table (app,
256                                            session_endpoint_fib_proto (sep));
257   listener = session_lookup_listener (table_index, sep);
258   if (listener)
259     {
260       server = application_get (listener->app_index);
261       if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
262         return app_connect_redirect (server, mp);
263     }
264
265   /*
266    * Not connecting to a local server, propagate to transport
267    */
268   if (application_open_session (app, sep, api_context))
269     return VNET_API_ERROR_SESSION_CONNECT;
270   return 0;
271 }
272
273 /**
274  * unformat a vnet URI
275  *
276  * fifo://name
277  * tcp://ip46-addr:port
278  * udp://ip46-addr:port
279  *
280  * u8 ip46_address[16];
281  * u16  port_in_host_byte_order;
282  * stream_session_type_t sst;
283  * u8 *fifo_name;
284  *
285  * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
286  *              &sst, &port, &fifo_name))
287  *  etc...
288  *
289  */
290 uword
291 unformat_vnet_uri (unformat_input_t * input, va_list * args)
292 {
293   session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
294
295   if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
296                 &sep->port))
297     {
298       sep->transport_proto = TRANSPORT_PROTO_TCP;
299       sep->port = clib_host_to_net_u16 (sep->port);
300       sep->is_ip4 = 1;
301       return 1;
302     }
303   if (unformat (input, "udp://%U/%d", unformat_ip4_address, &sep->ip.ip4,
304                 &sep->port))
305     {
306       sep->transport_proto = TRANSPORT_PROTO_UDP;
307       sep->port = clib_host_to_net_u16 (sep->port);
308       sep->is_ip4 = 1;
309       return 1;
310     }
311   if (unformat (input, "udp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
312                 &sep->port))
313     {
314       sep->transport_proto = TRANSPORT_PROTO_UDP;
315       sep->port = clib_host_to_net_u16 (sep->port);
316       sep->is_ip4 = 0;
317       return 1;
318     }
319   if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &sep->ip.ip6,
320                 &sep->port))
321     {
322       sep->transport_proto = TRANSPORT_PROTO_TCP;
323       sep->port = clib_host_to_net_u16 (sep->port);
324       sep->is_ip4 = 0;
325       return 1;
326     }
327
328   return 0;
329 }
330
331 static u8 *cache_uri;
332 static session_endpoint_t *cache_sep;
333
334 int
335 parse_uri (char *uri, session_endpoint_t * sep)
336 {
337   unformat_input_t _input, *input = &_input;
338
339   if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
340     {
341       *sep = *cache_sep;
342       return 0;
343     }
344
345   /* Make sure */
346   uri = (char *) format (0, "%s%c", uri, 0);
347
348   /* Parse uri */
349   unformat_init_string (input, uri, strlen (uri));
350   if (!unformat (input, "%U", unformat_vnet_uri, sep))
351     {
352       unformat_free (input);
353       return VNET_API_ERROR_INVALID_VALUE;
354     }
355   unformat_free (input);
356
357   vec_free (cache_uri);
358   cache_uri = (u8 *) uri;
359   if (cache_sep)
360     clib_mem_free (cache_sep);
361   cache_sep = clib_mem_alloc (sizeof (*sep));
362   *cache_sep = *sep;
363
364   return 0;
365 }
366
367 static int
368 session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
369 {
370   app_namespace_t *app_ns;
371   if (vec_len (namespace_id) == 0)
372     {
373       /* Use default namespace */
374       *app_ns_index = 0;
375       return 0;
376     }
377
378   *app_ns_index = app_namespace_index_from_id (namespace_id);
379   if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
380     return VNET_API_ERROR_APP_INVALID_NS;
381   app_ns = app_namespace_get (*app_ns_index);
382   if (!app_ns)
383     return VNET_API_ERROR_APP_INVALID_NS;
384   if (app_ns->ns_secret != secret)
385     return VNET_API_ERROR_APP_WRONG_NS_SECRET;
386   return 0;
387 }
388
389 /**
390  * Attach application to vpp
391  *
392  * Allocates a vpp app, i.e., a structure that keeps back pointers
393  * to external app and a segment manager for shared memory fifo based
394  * communication with the external app.
395  */
396 clib_error_t *
397 vnet_application_attach (vnet_app_attach_args_t * a)
398 {
399   application_t *app = 0;
400   segment_manager_t *sm;
401   u8 *seg_name;
402   u64 secret;
403   u32 app_ns_index = 0;
404   int rv;
405
406   app = application_lookup (a->api_client_index);
407   if (app)
408     return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
409                                    0, "app already attached");
410
411   secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
412   if ((rv = session_validate_namespace (a->namespace_id, secret,
413                                         &app_ns_index)))
414     return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
415   a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
416   app = application_new ();
417   if ((rv = application_init (app, a->api_client_index, a->options,
418                               a->session_cb_vft)))
419     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
420
421   a->app_event_queue_address = pointer_to_uword (app->event_queue);
422   sm = segment_manager_get (app->first_segment_manager);
423   segment_manager_get_segment_info (sm->segment_indices[0],
424                                     &seg_name, &a->segment_size);
425
426   if (application_is_proxy (app))
427     application_setup_proxy (app);
428
429   a->segment_name_length = vec_len (seg_name);
430   a->segment_name = seg_name;
431   ASSERT (vec_len (a->segment_name) <= 128);
432   a->app_index = app->index;
433   return 0;
434 }
435
436 /**
437  * Detach application from vpp
438  */
439 int
440 vnet_application_detach (vnet_app_detach_args_t * a)
441 {
442   application_t *app;
443   app = application_get_if_valid (a->app_index);
444
445   if (!app)
446     {
447       clib_warning ("app not attached");
448       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
449     }
450
451   application_del (app);
452   return 0;
453 }
454
455 int
456 vnet_bind_uri (vnet_bind_args_t * a)
457 {
458   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
459   int rv;
460
461   rv = parse_uri (a->uri, &sep);
462   if (rv)
463     return rv;
464
465   return vnet_bind_i (a->app_index, &sep, &a->handle);
466 }
467
468 int
469 vnet_unbind_uri (vnet_unbind_args_t * a)
470 {
471   stream_session_t *listener;
472   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
473   int rv;
474
475   rv = parse_uri (a->uri, &sep);
476   if (rv)
477     return rv;
478
479   /* NOTE: only default table supported for uri */
480   listener = session_lookup_listener (0, &sep);
481   if (!listener)
482     return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
483
484   return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
485 }
486
487 clib_error_t *
488 vnet_connect_uri (vnet_connect_args_t * a)
489 {
490   session_endpoint_t sep_null = SESSION_ENDPOINT_NULL;
491   int rv;
492
493   /* Parse uri */
494   a->sep = sep_null;
495   rv = parse_uri (a->uri, &a->sep);
496   if (rv)
497     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
498   if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
499     return clib_error_return_code (0, rv, 0, "connect failed");
500   return 0;
501 }
502
503 int
504 vnet_disconnect_session (vnet_disconnect_args_t * a)
505 {
506   u32 index, thread_index;
507   stream_session_t *s;
508
509   session_parse_handle (a->handle, &index, &thread_index);
510   s = session_get_if_valid (index, thread_index);
511
512   if (!s || s->app_index != a->app_index)
513     return VNET_API_ERROR_INVALID_VALUE;
514
515   /* We're peeking into another's thread pool. Make sure */
516   ASSERT (s->session_index == index);
517
518   session_send_session_evt_to_thread (a->handle, FIFO_EVENT_DISCONNECT,
519                                       thread_index);
520   return 0;
521 }
522
523 clib_error_t *
524 vnet_bind (vnet_bind_args_t * a)
525 {
526   int rv;
527   if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
528     return clib_error_return_code (0, rv, 0, "bind failed");
529   return 0;
530 }
531
532 clib_error_t *
533 vnet_unbind (vnet_unbind_args_t * a)
534 {
535   int rv;
536   if ((rv = vnet_unbind_i (a->app_index, a->handle)))
537     return clib_error_return_code (0, rv, 0, "unbind failed");
538   return 0;
539 }
540
541 clib_error_t *
542 vnet_connect (vnet_connect_args_t * a)
543 {
544   int rv;
545   if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
546     return clib_error_return_code (0, rv, 0, "connect failed");
547   return 0;
548 }
549
550 /*
551  * fd.io coding-style-patch-verification: ON
552  *
553  * Local Variables:
554  * eval: (c-set-style "gnu")
555  * End:
556  */