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 */
28 builtin_session_create_callback (stream_session_t * s)
30 /* Simple version: declare session ready-to-go... */
31 s->session_state = SESSION_STATE_READY;
36 builtin_session_disconnect_callback (stream_session_t * s)
38 stream_session_disconnect (s);
42 builtin_server_rx_callback (stream_session_t * s)
44 svm_fifo_t *rx_fifo, *tx_fifo;
45 u32 this_transfer, max_deq, max_enq;
48 session_fifo_event_t evt;
49 unix_shared_memory_queue_t *q;
51 my_copy_buffer = copy_buffers[s->thread_index];
52 rx_fifo = s->server_rx_fifo;
53 tx_fifo = s->server_tx_fifo;
55 max_deq = svm_fifo_max_dequeue (rx_fifo);
56 max_enq = svm_fifo_max_enqueue (tx_fifo);
57 this_transfer = max_enq < max_deq ? max_enq : max_deq;
59 vec_validate (my_copy_buffer, this_transfer - 1);
60 _vec_len (my_copy_buffer) = this_transfer;
62 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, 0, this_transfer,
64 ASSERT (actual_transfer == this_transfer);
65 actual_transfer = svm_fifo_enqueue_nowait (tx_fifo, 0, this_transfer,
67 ASSERT (actual_transfer == this_transfer);
69 copy_buffers[s->thread_index] = my_copy_buffer;
71 if (svm_fifo_set_event (tx_fifo))
73 /* Fabricate TX event, send to ourselves */
75 evt.event_type = FIFO_EVENT_SERVER_TX;
77 q = session_manager_get_vpp_event_queue (s->thread_index);
78 unix_shared_memory_queue_add (q, (u8 *) & evt,
79 0 /* do wait for mutex */ );
86 static session_cb_vft_t builtin_server = {
87 .session_accept_callback = builtin_session_create_callback,
88 .session_disconnect_callback = builtin_session_disconnect_callback,
89 .builtin_server_rx_callback = builtin_server_rx_callback
94 bind_builtin_uri_server (u8 * uri)
96 vnet_bind_args_t _a, *a = &_a;
97 char segment_name[128];
98 u32 segment_name_length;
102 segment_name_length = ARRAY_LEN (segment_name);
104 memset (a, 0, sizeof (*a));
105 memset (options, 0, sizeof (options));
107 a->uri = (char *) uri;
108 a->api_client_index = ~0; /* built-in server */
109 a->segment_name = segment_name;
110 a->segment_name_length = segment_name_length;
111 a->session_cb_vft = &builtin_server;
113 options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
114 options[SESSION_OPTIONS_SEGMENT_SIZE] = (2 << 30); /*$$$$ config / arg */
115 a->options = options;
117 rv = vnet_bind_uri (a);
123 unbind_builtin_uri_server (u8 * uri)
127 rv = vnet_unbind_uri ((char *) uri, ~0 /* client_index */ );
132 static clib_error_t *
133 builtin_server_init (vlib_main_t * vm)
135 vlib_thread_main_t *vtm = vlib_get_thread_main ();
138 num_threads = 1 /* main thread */ + vtm->n_threads;
140 vec_validate (copy_buffers, num_threads - 1);
144 VLIB_INIT_FUNCTION (builtin_server_init);
146 static clib_error_t *
147 builtin_uri_bind_command_fn (vlib_main_t * vm,
148 unformat_input_t * input,
149 vlib_cli_command_t * cmd)
154 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
156 if (unformat (input, "uri %s", &uri))
163 return clib_error_return (0, "uri to bind not specified...");
165 rv = bind_builtin_uri_server (uri);
175 return clib_error_return (0, "bind_uri_server returned %d", rv);
183 VLIB_CLI_COMMAND (builtin_uri_bind_command, static) =
185 .path = "builtin uri bind",
186 .short_help = "builtin uri bind",
187 .function = builtin_uri_bind_command_fn,
191 static clib_error_t *
192 builtin_uri_unbind_command_fn (vlib_main_t * vm,
193 unformat_input_t * input,
194 vlib_cli_command_t * cmd)
199 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
201 if (unformat (input, "uri %s", &uri))
208 return clib_error_return (0, "uri to unbind not specified...");
210 rv = unbind_builtin_uri_server (uri);
220 return clib_error_return (0, "unbind_uri_server returned %d", rv);
228 VLIB_CLI_COMMAND (builtin_uri_unbind_command, static) =
230 .path = "builtin uri unbind",
231 .short_help = "builtin uri unbind",
232 .function = builtin_uri_unbind_command_fn,
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "gnu")