2 * Copyright (c) 2017 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/session/application.h>
17 #include <vnet/session/session.h>
20 * Pool from which we allocate all applications
22 static application_t *app_pool;
25 * Hash table of apps by api client index
27 static uword *app_by_api_client_index;
30 application_api_queue_is_full (application_t * app)
32 unix_shared_memory_queue_t *q;
34 /* builtin servers are always OK */
35 if (app->api_client_index == ~0)
38 q = vl_api_client_index_to_input_queue (app->api_client_index);
42 if (q->cursize == q->maxsize)
48 application_table_add (application_t * app)
50 hash_set (app_by_api_client_index, app->api_client_index, app->index);
54 application_table_del (application_t * app)
56 hash_unset (app_by_api_client_index, app->api_client_index);
60 application_lookup (u32 api_client_index)
63 p = hash_get (app_by_api_client_index, api_client_index);
65 return application_get (p[0]);
71 application_del (application_t * app)
73 session_manager_main_t *smm = vnet_get_session_manager_main ();
74 api_main_t *am = &api_main;
76 session_manager_t *sm;
78 if (app->mode == APP_SERVER)
80 sm = session_manager_get (app->session_manager_index);
81 session_manager_del (smm, sm);
84 /* Free the event fifo in the /vpe-api shared-memory segment */
85 oldheap = svm_push_data_heap (am->vlib_rp);
87 unix_shared_memory_queue_free (app->event_queue);
88 svm_pop_heap (oldheap);
90 application_table_del (app);
92 pool_put (app_pool, app);
96 application_verify_cb_fns (application_type_t type, session_cb_vft_t * cb_fns)
98 if (type == APP_SERVER && cb_fns->session_accept_callback == 0)
99 clib_warning ("No accept callback function provided");
100 if (type == APP_CLIENT && cb_fns->session_connected_callback == 0)
101 clib_warning ("No session connected callback function provided");
102 if (cb_fns->session_disconnect_callback == 0)
103 clib_warning ("No session disconnect callback function provided");
104 if (cb_fns->session_reset_callback == 0)
105 clib_warning ("No session reset callback function provided");
109 application_new (application_type_t type, session_type_t sst,
110 u32 api_client_index, u32 flags, session_cb_vft_t * cb_fns)
112 session_manager_main_t *smm = vnet_get_session_manager_main ();
113 api_main_t *am = &api_main;
116 session_manager_t *sm;
118 pool_get (app_pool, app);
119 memset (app, 0, sizeof (*app));
121 /* Allocate event fifo in the /vpe-api shared-memory segment */
122 oldheap = svm_push_data_heap (am->vlib_rp);
124 /* Allocate server event queue */
126 unix_shared_memory_queue_init (128 /* nels $$$$ config */ ,
127 sizeof (session_fifo_event_t),
128 0 /* consumer pid */ ,
130 /* (do not) signal when queue non-empty */
133 svm_pop_heap (oldheap);
135 /* If a server, allocate session manager */
136 if (type == APP_SERVER)
138 pool_get (smm->session_managers, sm);
139 memset (sm, 0, sizeof (*sm));
141 app->session_manager_index = sm - smm->session_managers;
143 else if (type == APP_CLIENT)
145 /* Allocate connect session manager if needed */
146 if (smm->connect_manager_index[sst] == INVALID_INDEX)
147 connects_session_manager_init (smm, sst);
148 app->session_manager_index = smm->connect_manager_index[sst];
152 app->index = application_get_index (app);
153 app->session_type = sst;
154 app->api_client_index = api_client_index;
156 app->cb_fns = *cb_fns;
158 /* Check that the obvious things are properly set up */
159 application_verify_cb_fns (type, cb_fns);
161 /* Add app to lookup by api_client_index table */
162 application_table_add (app);
168 application_get (u32 index)
170 return pool_elt_at_index (app_pool, index);
174 application_get_if_valid (u32 index)
176 if (pool_is_free_index (app_pool, index))
179 return pool_elt_at_index (app_pool, index);
183 application_get_index (application_t * app)
185 return app - app_pool;
189 application_server_init (application_t * server, u32 segment_size,
190 u32 add_segment_size, u32 rx_fifo_size,
191 u32 tx_fifo_size, u8 ** segment_name)
193 session_manager_main_t *smm = vnet_get_session_manager_main ();
194 session_manager_t *sm;
197 sm = session_manager_get (server->session_manager_index);
199 /* Add first segment */
200 if ((rv = session_manager_add_first_segment (smm, sm, segment_size,
206 /* Setup session manager */
207 sm->add_segment_size = add_segment_size;
208 sm->rx_fifo_size = rx_fifo_size;
209 sm->tx_fifo_size = tx_fifo_size;
210 sm->add_segment = sm->add_segment_size != 0;
215 format_application_server (u8 * s, va_list * args)
217 application_t *srv = va_arg (*args, application_t *);
218 int verbose = va_arg (*args, int);
219 vl_api_registration_t *regp;
220 stream_session_t *listener;
221 u8 *server_name, *str, *seg_name;
227 s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "Server",
228 "Segment", "API Client", "Cookie");
230 s = format (s, "%-40s%-20s", "Connection", "Server");
235 regp = vl_api_client_index_to_registration (srv->api_client_index);
237 server_name = format (0, "builtin-%d%c", srv->index, 0);
239 server_name = regp->name;
241 listener = stream_session_listener_get (srv->session_type,
243 str = format (0, "%U", format_stream_session, listener, verbose);
245 session_manager_get_segment_info (listener->server_segment_index, &seg_name,
249 s = format (s, "%-40s%-20s%-20s%-10d%-10d", str, server_name,
250 seg_name, srv->api_client_index, srv->accept_cookie);
253 s = format (s, "%-40s%-20s", str, server_name);
258 format_application_client (u8 * s, va_list * args)
260 application_t *client = va_arg (*args, application_t *);
261 int verbose = va_arg (*args, int);
262 stream_session_t *session;
270 format (s, "%-40s%-20s%-10s", "Connection", "Segment",
273 s = format (s, "%-40s", "Connection");
278 session = stream_session_get (client->session_index, client->thread_index);
279 str = format (0, "%U", format_stream_session, session, verbose);
281 session_manager_get_segment_info (session->server_segment_index, &seg_name,
285 s = format (s, "%-40s%-20s%-10d%", str, seg_name,
286 client->api_client_index);
289 s = format (s, "%-40s", str);
293 static clib_error_t *
294 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
295 vlib_cli_command_t * cmd)
297 session_manager_main_t *smm = &session_manager_main;
303 if (!smm->is_enabled)
305 clib_error_return (0, "session layer is not enabled");
308 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
310 if (unformat (input, "server"))
312 else if (unformat (input, "client"))
314 else if (unformat (input, "verbose"))
322 if (pool_elts (app_pool))
324 vlib_cli_output (vm, "%U", format_application_server,
328 pool_foreach (app, app_pool,
330 if (app->mode == APP_SERVER)
331 vlib_cli_output (vm, "%U", format_application_server, app,
337 vlib_cli_output (vm, "No active server bindings");
342 if (pool_elts (app_pool))
344 vlib_cli_output (vm, "%U", format_application_client,
348 pool_foreach (app, app_pool,
350 if (app->mode == APP_CLIENT)
351 vlib_cli_output (vm, "%U", format_application_client, app,
357 vlib_cli_output (vm, "No active client bindings");
364 VLIB_CLI_COMMAND (show_app_command, static) =
367 .short_help = "show app [server|client] [verbose]",
368 .function = show_app_command_fn,
373 * fd.io coding-style-patch-verification: ON
376 * eval: (c-set-style "gnu")