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