2 *------------------------------------------------------------------
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <sys/ioctl.h>
24 #include <vppinfra/byte_order.h>
29 #include <vlibsocket/api.h>
30 #include <vlibmemory/api.h>
32 #include <vlibsocket/vl_socket_msg_enum.h> /* enumerate all vlib messages */
34 #define vl_typedefs /* define message structures */
35 #include <vlibsocket/vl_socket_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vlibsocket/vl_socket_api_h.h>
44 /* instantiate all the endian swap functions we know about */
46 #include <vlibsocket/vl_socket_api_h.h>
49 socket_main_t socket_main;
51 void dump_socket_clients (vlib_main_t *vm, api_main_t *am)
53 vl_api_registration_t *reg;
54 socket_main_t *sm = &socket_main;
55 unix_main_t *um = &unix_main;
59 * Must have at least one active client, not counting the
60 * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket
62 if (pool_elts (sm->registration_pool) < 2)
65 vlib_cli_output (vm, "TCP socket clients");
66 vlib_cli_output (vm, "%16s %8s", "Name", "Fildesc");
67 pool_foreach (reg, sm->registration_pool,
69 if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) {
70 f = pool_elt_at_index (um->file_pool, reg->unix_file_index);
71 vlib_cli_output (vm, "%16s %8d",
72 reg->name, f->file_descriptor);
78 vl_socket_api_send (vl_api_registration_t *rp, u8 *elem)
80 u32 nbytes = 4; /* for the length... */
81 u16 msg_id = ntohs(*(u16 *)elem);
84 api_main_t *am = &api_main;
86 ASSERT(rp->registration_type > REGISTRATION_TYPE_SHMEM);
88 if (msg_id >= vec_len(am->api_trace_cfg)) {
89 clib_warning("id out of range: %d", msg_id);
90 vl_msg_api_free((void *)elem);
94 msg_length = am->api_trace_cfg[msg_id].size;
96 tmp = clib_host_to_net_u32 (nbytes);
98 vl_socket_add_pending_output (rp->unix_file_index
99 + unix_main.file_pool,
100 rp->vl_api_registration_pool_index
101 + socket_main.registration_pool,
104 vl_socket_add_pending_output (rp->unix_file_index
105 + unix_main.file_pool,
106 rp->vl_api_registration_pool_index
107 + socket_main.registration_pool,
109 vl_msg_api_free((void *)elem);
112 void vl_socket_api_send_with_data (vl_api_registration_t *rp,
113 u8 *elem, u8 *data_vector)
115 u32 nbytes = 4; /* for the length... */
116 u16 msg_id = ntohs(*(u16 *)elem);
119 api_main_t *am = &api_main;
121 ASSERT(rp->registration_type > REGISTRATION_TYPE_SHMEM);
123 if (msg_id >= vec_len(am->api_trace_cfg)) {
124 clib_warning("id out of range: %d", msg_id);
125 vec_free(data_vector);
126 vl_msg_api_free((void *)elem);
130 msg_length = am->api_trace_cfg[msg_id].size;
131 nbytes += msg_length;
132 nbytes += vec_len(data_vector);
134 /* Length in network byte order */
135 tmp = clib_host_to_net_u32(nbytes);
137 vl_socket_add_pending_output (rp->unix_file_index
138 + unix_main.file_pool,
139 rp->vl_api_registration_pool_index
140 + socket_main.registration_pool,
143 vl_socket_add_pending_output (rp->unix_file_index
144 + unix_main.file_pool,
145 rp->vl_api_registration_pool_index
146 + socket_main.registration_pool,
148 vl_socket_add_pending_output (rp->unix_file_index
149 + unix_main.file_pool,
150 rp->vl_api_registration_pool_index
151 + socket_main.registration_pool,
152 data_vector, vec_len(data_vector));
153 vl_msg_api_free((void *)elem);
157 vl_socket_api_send_with_length_internal (vl_api_registration_t *rp,
158 u8 *elem, u32 msg_length, int free)
160 u32 nbytes = 4; /* for the length... */
161 u16 msg_id = ntohs(*(u16 *)elem);
163 api_main_t *am = &api_main;
165 ASSERT(rp->registration_type > REGISTRATION_TYPE_SHMEM);
167 if (msg_id >= vec_len(am->api_trace_cfg)) {
168 clib_warning("id out of range: %d", msg_id);
170 vl_msg_api_free((void *)elem);
174 nbytes += msg_length;
176 /* Length in network byte order */
177 tmp = clib_host_to_net_u32(nbytes);
179 vl_socket_add_pending_output (rp->unix_file_index
180 + unix_main.file_pool,
181 rp->vl_api_registration_pool_index
182 + socket_main.registration_pool,
185 vl_socket_add_pending_output (rp->unix_file_index
186 + unix_main.file_pool,
187 rp->vl_api_registration_pool_index
188 + socket_main.registration_pool,
191 vl_msg_api_free((void *)elem);
194 void vl_socket_api_send_with_length (vl_api_registration_t *rp,
195 u8 *elem, u32 msg_length)
197 vl_socket_api_send_with_length_internal(rp,elem,msg_length, 1 /* free */);
200 void vl_socket_api_send_with_length_no_free (vl_api_registration_t *rp,
201 u8 *elem, u32 msg_length)
203 vl_socket_api_send_with_length_internal(rp,elem,msg_length, 0 /* free */);
206 void vl_free_socket_registration_index (u32 pool_index)
208 vl_api_registration_t *rp;
209 if (pool_is_free_index (socket_main.registration_pool, pool_index)) {
210 clib_warning ("main pool index %d already free", pool_index);
213 rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
215 ASSERT(rp->registration_type != REGISTRATION_TYPE_FREE);
217 vec_free (rp->unprocessed_input);
218 vec_free (rp->output_vector);
219 rp->registration_type = REGISTRATION_TYPE_FREE;
220 pool_put (socket_main.registration_pool, rp);
224 socket_process_msg (unix_file_t *uf, vl_api_registration_t *rp, i8 *input_v)
226 u8 *the_msg = (u8 *)(input_v+sizeof(u32));
227 socket_main.current_uf = uf;
228 socket_main.current_rp = rp;
229 vl_msg_api_socket_handler (the_msg);
230 socket_main.current_uf = 0;
231 socket_main.current_rp = 0;
234 clib_error_t * vl_socket_read_ready (unix_file_t * uf)
236 unix_main_t * um = &unix_main;
237 vl_api_registration_t * rp;
241 u32 save_input_buffer_length = vec_len(socket_main.input_buffer);
243 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
245 n = read (uf->file_descriptor, socket_main.input_buffer,
246 vec_len(socket_main.input_buffer));
248 if (n <= 0 && errno != EAGAIN) {
249 unix_file_del (um, uf);
251 if (!pool_is_free (socket_main.registration_pool, rp)) {
252 u32 index = rp - socket_main.registration_pool;
253 vl_free_socket_registration_index (index);
255 clib_warning("client index %d already free?",
256 rp->vl_api_registration_pool_index);
261 _vec_len(socket_main.input_buffer) = n;
264 * Look for bugs here. This code is tricky because
265 * data read from a stream socket does honor message
266 * boundaries. In the case of a long message (>4K bytes)
267 * we have to do (at least) 2 reads, etc.
270 if (vec_len(rp->unprocessed_input)) {
271 vec_append(rp->unprocessed_input, socket_main.input_buffer);
272 msg_buffer = rp->unprocessed_input;
273 msg_len = rp->unprocessed_msg_length;
275 msg_buffer = socket_main.input_buffer;
280 /* Length may be split across two reads */
281 if (vec_len(msg_buffer) < sizeof (u32))
284 /* total length, including msg_len itself, in network byte order */
285 msg_len = clib_net_to_host_u32 (*((u32 *)msg_buffer));
288 /* Happens if the client sent msg_len == 0 */
290 clib_warning ("msg_len == 0");
294 /* We don't have the entire message yet. */
295 if (msg_len > vec_len(msg_buffer)) {
298 * if we were using the shared input buffer,
301 if (msg_buffer == socket_main.input_buffer) {
302 ASSERT(vec_len(rp->unprocessed_input) == 0);
303 vec_validate (rp->unprocessed_input, vec_len (msg_buffer)-1);
304 memcpy (rp->unprocessed_input, msg_buffer, vec_len(msg_buffer));
305 _vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
307 _vec_len(socket_main.input_buffer) = save_input_buffer_length;
308 rp->unprocessed_msg_length = msg_len;
312 socket_process_msg (uf, rp, msg_buffer);
314 vec_delete(msg_buffer, msg_len, 0);
316 _vec_len(msg_buffer) = 0;
319 rp->unprocessed_msg_length = 0;
323 _vec_len(socket_main.input_buffer) = save_input_buffer_length;
329 vl_socket_add_pending_output (unix_file_t * uf,
330 vl_api_registration_t * rp,
334 unix_main_t * um = &unix_main;
336 vec_add (rp->output_vector, buffer, buffer_bytes);
337 if (vec_len (rp->output_vector) > 0) {
338 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
339 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
341 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
346 socket_del_pending_output (unix_file_t * uf,
347 vl_api_registration_t * rp,
350 unix_main_t * um = &unix_main;
352 vec_delete (rp->output_vector, n_bytes, 0);
353 if (vec_len (rp->output_vector) <= 0)
355 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
356 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
358 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
363 vl_socket_write_ready (unix_file_t * uf)
365 unix_main_t * um = &unix_main;
366 vl_api_registration_t * rp;
369 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
371 /* Flush output vector. */
372 n = write (uf->file_descriptor,
373 rp->output_vector, vec_len (rp->output_vector));
377 clib_warning ("write error, close the file...\n");
379 unix_file_del (um, uf);
381 vl_free_socket_registration_index (rp - socket_main.registration_pool);
386 socket_del_pending_output (uf, rp, n);
391 clib_error_t * vl_socket_error_ready (unix_file_t * uf)
393 vl_api_registration_t * rp;
394 unix_main_t * um = &unix_main;
396 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
397 unix_file_del (um, uf);
398 vl_free_socket_registration_index (rp - socket_main.registration_pool);
403 void socksvr_file_add (unix_main_t *um, int fd)
405 vl_api_registration_t *rp;
406 unix_file_t template = {0};
408 pool_get (socket_main.registration_pool, rp);
409 memset(rp, 0, sizeof(*rp));
411 template.read_function = vl_socket_read_ready;
412 template.write_function = vl_socket_write_ready;
413 template.error_function = vl_socket_error_ready;
414 template.file_descriptor = fd;
415 template.private_data = rp - socket_main.registration_pool;
417 rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER;
418 rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
419 rp->unix_file_index = unix_file_add (um, &template);
422 static clib_error_t * socksvr_accept_ready (unix_file_t * uf)
424 unix_main_t * um = &unix_main;
425 struct sockaddr_in client_addr;
429 client_len = sizeof(client_addr);
432 * Supposedly acquires the non-blocking attrib from the
435 client_fd = accept (uf->file_descriptor,
436 (struct sockaddr *)&client_addr,
437 (socklen_t *)&client_len);
440 return clib_error_return_unix (0, "socksvr_accept_ready: accept");
442 socksvr_file_add (um, client_fd);
446 static clib_error_t * socksvr_bogus_write (unix_file_t * uf)
448 clib_warning ("why am I here?");
453 * vl_api_sockclnt_create_t_handler
455 void vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t *mp)
457 vl_api_registration_t *regp;
458 vl_api_sockclnt_create_reply_t *rp;
461 regp = socket_main.current_rp;
463 ASSERT(regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
465 regp->name = format(0, "%s%c", mp->name, 0);
467 rp = vl_msg_api_alloc(sizeof(*rp));
468 rp->_vl_msg_id = htons(VL_API_SOCKCLNT_CREATE_REPLY);
469 rp->handle = (uword)regp;
470 rp->index = (uword)regp->vl_api_registration_pool_index;
471 rp->context = mp->context;
472 rp->response = htonl(rv);
474 vl_msg_api_send (regp, (u8 *)rp);
478 * vl_api_sockclnt_delete_t_handler
480 void vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t *mp)
482 vl_api_registration_t *regp;
483 vl_api_sockclnt_delete_reply_t *rp;
485 if (!pool_is_free_index(socket_main.registration_pool, mp->index)) {
486 regp = pool_elt_at_index(socket_main.registration_pool, mp->index);
488 rp = vl_msg_api_alloc(sizeof(*rp));
489 rp->_vl_msg_id = htons(VL_API_SOCKCLNT_DELETE_REPLY);
490 rp->handle = mp->handle;
491 rp->response = htonl(1);
493 vl_msg_api_send (regp, (u8 *)rp);
495 unix_file_del (&unix_main,
496 unix_main.file_pool + regp->unix_file_index);
498 vl_free_socket_registration_index (mp->index);
500 clib_warning("unknown client ID %d", mp->index);
504 #define foreach_vlib_api_msg \
505 _(SOCKCLNT_CREATE, sockclnt_create) \
506 _(SOCKCLNT_DELETE, sockclnt_delete)
508 static clib_error_t *socksvr_api_init (vlib_main_t * vm)
510 unix_main_t * um = &unix_main;
511 unix_file_t template = {0};
515 struct sockaddr_in serv_addr;
516 vl_api_registration_t *rp;
521 vl_msg_api_set_handlers(VL_API_##N, #n, \
522 vl_api_##n##_t_handler, \
524 vl_api_##n##_t_endian, \
525 vl_api_##n##_t_print, \
526 sizeof(vl_api_##n##_t), 1);
527 foreach_vlib_api_msg;
530 vec_resize(socket_main.input_buffer, 4096);
532 /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */
533 sockfd = socket(AF_INET, SOCK_STREAM, 0);
536 return clib_error_return_unix (0, "socket");
539 rv = ioctl (sockfd, FIONBIO, &one);
541 return clib_error_return_unix (0, "FIONBIO");
544 rv = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
546 return clib_error_return_unix (0, "SO_REUSEADDR");
549 bzero((char *) &serv_addr, sizeof(serv_addr));
550 serv_addr.sin_family = AF_INET;
552 if (socket_main.bind_address)
553 bind_address = socket_main.bind_address;
555 bind_address = INADDR_LOOPBACK;
557 if (socket_main.portno)
558 portno = socket_main.portno;
560 portno = SOCKSVR_DEFAULT_PORT;
562 serv_addr.sin_port = clib_host_to_net_u16 (portno);
563 serv_addr.sin_addr.s_addr = clib_host_to_net_u32(bind_address);
565 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
566 return clib_error_return_unix (0, "bind");
569 rv = listen(sockfd,5);
571 return clib_error_return_unix (0, "listen");
574 pool_get (socket_main.registration_pool, rp);
575 memset(rp, 0, sizeof(*rp));
577 rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
579 template.read_function = socksvr_accept_ready;
580 template.write_function = socksvr_bogus_write;
581 template.file_descriptor = sockfd;
582 template.private_data = rp - socket_main.registration_pool;
584 rp->unix_file_index = unix_file_add (um, &template);
588 static clib_error_t *
589 socket_exit (vlib_main_t * vm)
591 unix_main_t * um = &unix_main;
592 vl_api_registration_t *rp;
594 /* Defensive driving in case something wipes out early */
595 if (socket_main.registration_pool) {
597 pool_foreach (rp, socket_main.registration_pool, ({
598 unix_file_del (um, um->file_pool + rp->unix_file_index);
599 index = rp->vl_api_registration_pool_index;
600 vl_free_socket_registration_index (index);
607 VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
609 static clib_error_t *
610 socksvr_config (vlib_main_t * vm, unformat_input_t * input)
614 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
616 if (unformat (input, "port %d", &portno)) {
617 socket_main.portno = portno;
619 return clib_error_return (0, "unknown input '%U'",
620 format_unformat_error, input);
623 return socksvr_api_init (vm);
626 VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
628 /* argument in host byte order */
629 void socksvr_set_port (u16 port)
631 socket_main.portno = port;
634 /* argument in host byte order */
635 void socksvr_set_bind_address (u32 bind_address)
637 socket_main.bind_address = bind_address;
641 vlibsocket_init (vlib_main_t * vm)
646 VLIB_INIT_FUNCTION (vlibsocket_init);