FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
builtin_server.c
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015-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:
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 
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
20 
21 typedef struct
22 {
27 
29 
30 
31 int
33 {
35  clib_warning ("called...");
36 
37  bsm->vpp_queue[s->thread_index] =
38  session_manager_get_vpp_event_queue (s->thread_index);
39  s->session_state = SESSION_STATE_READY;
40  return 0;
41 }
42 
43 void
45 {
46  clib_warning ("called...");
47 
48  vnet_disconnect_session (s->session_index, s->thread_index);
49 }
50 
51 void
53 {
54  clib_warning ("called.. ");
55 
57 }
58 
59 
60 int
62  stream_session_t * s, u8 is_fail)
63 {
64  clib_warning ("called...");
65  return -1;
66 }
67 
68 int
70  const u8 * seg_name, u32 seg_size)
71 {
72  clib_warning ("called...");
73  return -1;
74 }
75 
76 int
77 builtin_redirect_connect_callback (u32 client_index, void *mp)
78 {
79  clib_warning ("called...");
80  return -1;
81 }
82 
83 int
84 builtin_server_rx_callback (stream_session_t * s, session_fifo_event_t * e)
85 {
86  int n_written, bytes, total_copy_bytes;
87  int n_read;
88  svm_fifo_t *tx_fifo;
90  session_fifo_event_t evt;
91  static int serial_number = 0;
92 
93  bytes = e->enqueue_length;
94  if (PREDICT_FALSE (bytes <= 0))
95  {
96  clib_warning ("bizarre rx callback: bytes %d", bytes);
97  return 0;
98  }
99 
100  tx_fifo = s->server_tx_fifo;
101 
102  /* Number of bytes we're going to copy */
103  total_copy_bytes = (bytes < (tx_fifo->nitems - tx_fifo->cursize)) ? bytes :
104  tx_fifo->nitems - tx_fifo->cursize;
105 
106  if (PREDICT_FALSE (total_copy_bytes <= 0))
107  {
108  clib_warning ("no space in tx fifo, event had %d bytes", bytes);
109  return 0;
110  }
111 
112  vec_validate (bsm->rx_buf, total_copy_bytes - 1);
113  _vec_len (bsm->rx_buf) = total_copy_bytes;
114 
115  n_read = svm_fifo_dequeue_nowait (s->server_rx_fifo, 0, total_copy_bytes,
116  bsm->rx_buf);
117  ASSERT (n_read == total_copy_bytes);
118 
119  /*
120  * Echo back
121  */
122 
123  n_written = svm_fifo_enqueue_nowait (tx_fifo, 0, n_read, bsm->rx_buf);
124  ASSERT (n_written == total_copy_bytes);
125 
126  /* Fabricate TX event, send to vpp */
127  evt.fifo = tx_fifo;
128  evt.event_type = FIFO_EVENT_SERVER_TX;
129  evt.enqueue_length = total_copy_bytes;
130  evt.event_id = serial_number++;
131 
132  unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index], (u8 *) & evt,
133  0 /* do wait for mutex */ );
134 
135  return 0;
136 }
137 
139  .session_accept_callback = builtin_session_accept_callback,
140  .session_disconnect_callback = builtin_session_disconnect_callback,
141  .session_connected_callback = builtin_session_connected_callback,
142  .add_segment_callback = builtin_add_segment_callback,
143  .redirect_connect_callback = builtin_redirect_connect_callback,
144  .builtin_server_rx_callback = builtin_server_rx_callback,
145  .session_reset_callback = builtin_session_reset_callback
146 };
147 
148 static int
150 {
151  vnet_bind_args_t _a, *a = &_a;
153  char segment_name[128];
154  u32 num_threads;
156 
157  num_threads = 1 /* main thread */ + vtm->n_threads;
158  vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
159 
160  memset (a, 0, sizeof (*a));
161  memset (options, 0, sizeof (options));
162 
163  a->uri = "tcp://0.0.0.0/1234";
164  a->api_client_index = ~0;
165  a->session_cb_vft = &builtin_session_cb_vft;
166  a->options = options;
167  a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 10;
168  a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 64 << 10;
169  a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 64 << 10;
170  a->segment_name = segment_name;
171  a->segment_name_length = ARRAY_LEN (segment_name);
172 
173  return vnet_bind_uri (a);
174 }
175 
176 static clib_error_t *
178  unformat_input_t * input, vlib_cli_command_t * cmd)
179 {
180  int rv;
181 #if 0
183  {
184  if (unformat (input, "whatever %d", &whatever))
185  ;
186  else
187  return clib_error_return (0, "unknown input `%U'",
188  format_unformat_error, input);
189  }
190 #endif
191 
192  vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
193  rv = server_create (vm);
194  switch (rv)
195  {
196  case 0:
197  break;
198  default:
199  return clib_error_return (0, "server_create returned %d", rv);
200  }
201  return 0;
202 }
203 
204 /* *INDENT-OFF* */
205 VLIB_CLI_COMMAND (server_create_command, static) =
206 {
207  .path = "test server",
208  .short_help = "test server",
209  .function = server_create_command_fn,
210 };
211 /* *INDENT-ON* */
212 
213 /*
214 * fd.io coding-style-patch-verification: ON
215 *
216 * Local Variables:
217 * eval: (c-set-style "gnu")
218 * End:
219 */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
int svm_fifo_enqueue_nowait(svm_fifo_t *f, int pid, u32 max_bytes, u8 *copy_from_here)
Definition: svm_fifo.c:359
unix_shared_memory_queue_t ** vpp_queue
a
Definition: bitmap.h:516
int vnet_bind_uri(vnet_bind_args_t *a)
static clib_error_t * server_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
int builtin_redirect_connect_callback(u32 client_index, void *mp)
int vnet_disconnect_session(u32 session_index, u32 thread_index)
builtin_server_main_t builtin_server_main
struct _stream_session_t stream_session_t
struct _stream_session_cb_vft session_cb_vft_t
#define clib_error_return(e, args...)
Definition: error.h:111
unsigned long u64
Definition: types.h:89
void stream_session_cleanup(stream_session_t *s)
Cleanup transport and session state.
Definition: session.c:1206
volatile u32 cursize
Definition: svm_fifo.h:51
int unix_shared_memory_queue_add(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
void builtin_session_disconnect_callback(stream_session_t *s)
struct _unformat_input_t unformat_input_t
#define PREDICT_FALSE(x)
Definition: clib.h:97
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1306
static int server_create(vlib_main_t *vm)
static unix_shared_memory_queue_t * session_manager_get_vpp_event_queue(u32 thread_index)
Definition: session.h:242
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vlib_main
int builtin_add_segment_callback(u32 client_index, const u8 *seg_name, u32 seg_size)
vlib_main_t * vm
Definition: buffer.c:276
#define clib_warning(format, args...)
Definition: error.h:59
#define ARRAY_LEN(x)
Definition: clib.h:59
int builtin_session_accept_callback(stream_session_t *s)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
int builtin_server_rx_callback(stream_session_t *s, session_fifo_event_t *e)
int builtin_session_connected_callback(u32 client_index, stream_session_t *s, u8 is_fail)
u32 nitems
Definition: svm_fifo.h:52
unsigned char u8
Definition: types.h:56
void builtin_session_reset_callback(stream_session_t *s)
static session_cb_vft_t builtin_session_cb_vft
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
int svm_fifo_dequeue_nowait(svm_fifo_t *f, int pid, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:484
struct _vnet_bind_args_t vnet_bind_args_t
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
struct _unix_shared_memory_queue unix_shared_memory_queue_t