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:
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.
20 #include <vnet/udp/udp.h>
21 #include <vnet/session/session.h>
22 #include <vnet/session/application_interface.h>
24 /** per-worker built-in server copy buffers */
26 static int app_index = ~0;
29 builtin_session_create_callback (stream_session_t * s)
31 /* Simple version: declare session ready-to-go... */
32 s->session_state = SESSION_STATE_READY;
37 builtin_session_disconnect_callback (stream_session_t * s)
39 stream_session_disconnect (s);
43 builtin_session_reset_callback (stream_session_t * s)
45 clib_warning ("Reset session %U", format_stream_session, s, 2);
46 stream_session_cleanup (s);
50 builtin_session_connected_callback (u32 app_index, u32 api_context,
51 stream_session_t * s, u8 is_fail)
53 clib_warning ("called...");
58 builtin_server_rx_callback (stream_session_t * s)
60 svm_fifo_t *rx_fifo, *tx_fifo;
61 u32 this_transfer, max_deq, max_enq;
64 session_fifo_event_t evt;
65 unix_shared_memory_queue_t *q;
67 my_copy_buffer = copy_buffers[s->thread_index];
68 rx_fifo = s->server_rx_fifo;
69 tx_fifo = s->server_tx_fifo;
71 max_deq = svm_fifo_max_dequeue (rx_fifo);
72 max_enq = svm_fifo_max_enqueue (tx_fifo);
73 this_transfer = max_enq < max_deq ? max_enq : max_deq;
75 vec_validate (my_copy_buffer, this_transfer - 1);
76 _vec_len (my_copy_buffer) = this_transfer;
78 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, this_transfer,
80 ASSERT (actual_transfer == this_transfer);
81 actual_transfer = svm_fifo_enqueue_nowait (tx_fifo, this_transfer,
83 ASSERT (actual_transfer == this_transfer);
85 copy_buffers[s->thread_index] = my_copy_buffer;
87 if (svm_fifo_set_event (tx_fifo))
89 /* Fabricate TX event, send to ourselves */
91 evt.event_type = FIFO_EVENT_APP_TX;
92 q = session_manager_get_vpp_event_queue (s->thread_index);
93 unix_shared_memory_queue_add (q, (u8 *) & evt,
94 0 /* do wait for mutex */ );
101 static session_cb_vft_t builtin_server = {
102 .session_accept_callback = builtin_session_create_callback,
103 .session_connected_callback = builtin_session_connected_callback,
104 .session_disconnect_callback = builtin_session_disconnect_callback,
105 .builtin_server_rx_callback = builtin_server_rx_callback,
106 .session_reset_callback = builtin_session_reset_callback
111 attach_builtin_uri_server ()
113 vnet_app_attach_args_t _a, *a = &_a;
114 u8 segment_name[128];
115 u32 segment_name_length;
118 segment_name_length = ARRAY_LEN (segment_name);
120 memset (a, 0, sizeof (*a));
121 memset (options, 0, sizeof (options));
123 a->api_client_index = ~0;
124 a->segment_name = segment_name;
125 a->segment_name_length = segment_name_length;
126 a->session_cb_vft = &builtin_server;
128 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
129 options[APP_OPTIONS_SEGMENT_SIZE] = (2 << 30); /*$$$$ config / arg */
130 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
131 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 1024;
133 a->options = options;
135 if (vnet_application_attach (a))
138 app_index = a->app_index;
143 bind_builtin_uri_server (u8 * uri)
145 vnet_bind_args_t _a, *a = &_a;
148 rv = attach_builtin_uri_server ();
152 memset (a, 0, sizeof (*a));
153 a->uri = (char *) uri;
154 a->app_index = app_index;
156 rv = vnet_bind_uri (a);
162 unbind_builtin_uri_server (u8 * uri)
164 vnet_unbind_args_t _a, *a = &_a;
166 a->app_index = app_index;
167 a->uri = (char *) uri;
169 return vnet_unbind_uri (a);
172 static clib_error_t *
173 builtin_server_init (vlib_main_t * vm)
175 vlib_thread_main_t *vtm = vlib_get_thread_main ();
178 num_threads = 1 /* main thread */ + vtm->n_threads;
180 vec_validate (copy_buffers, num_threads - 1);
184 VLIB_INIT_FUNCTION (builtin_server_init);
186 static clib_error_t *
187 builtin_uri_bind_command_fn (vlib_main_t * vm,
188 unformat_input_t * input,
189 vlib_cli_command_t * cmd)
194 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
196 if (unformat (input, "uri %s", &uri))
203 return clib_error_return (0, "uri to bind not specified...");
205 vnet_session_enable_disable (vm, 1 /* turn on UDP, etc. */ );
207 rv = bind_builtin_uri_server (uri);
217 return clib_error_return (0, "bind_uri_server returned %d", rv);
225 VLIB_CLI_COMMAND (builtin_uri_bind_command, static) =
227 .path = "builtin uri bind",
228 .short_help = "builtin uri bind",
229 .function = builtin_uri_bind_command_fn,
233 static clib_error_t *
234 builtin_uri_unbind_command_fn (vlib_main_t * vm,
235 unformat_input_t * input,
236 vlib_cli_command_t * cmd)
241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
243 if (unformat (input, "uri %s", &uri))
250 return clib_error_return (0, "uri to unbind not specified...");
252 rv = unbind_builtin_uri_server (uri);
262 return clib_error_return (0, "unbind_uri_server returned %d", rv);
270 VLIB_CLI_COMMAND (builtin_uri_unbind_command, static) =
272 .path = "builtin uri unbind",
273 .short_help = "builtin uri unbind",
274 .function = builtin_uri_unbind_command_fn,
279 * fd.io coding-style-patch-verification: ON
282 * eval: (c-set-style "gnu")