#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vpp/app/version.h>
/* define message IDs */
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
/* define message IDs */
#include <sample/sample_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#define __plugin_msg_base sample_test_main.msg_id_base
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
unsigned char srv6_localsid_name[32] = "Sample-SRv6-LocalSID-plugin";
unsigned char keyword_str[32] = "new_srv6_localsid";
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
/* define message IDs */
#include <acl/acl_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#include <vnet/ip/ip.h>
#include <arpa/inet.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#include <vnet/ip/ip.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
/* define message IDs */
#include <flowprobe/flowprobe_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#include <flowprobe/flowprobe.h>
#include <vppinfra/byte_order.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <gtpu/gtpu.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#include <gtpu/gtpu.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#define __plugin_msg_base export_test_main.msg_id_base
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vnet/ip/ip6_hop_by_hop.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vppinfra/error.h>
#define __plugin_msg_base export_test_main.msg_id_base
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vnet/ip/ip6_hop_by_hop.h>
#include "ioam_cache.h"
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
/* define message IDs */
#include <ioam/lib-pot/pot_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#define __plugin_msg_base pot_test_main.msg_id_base
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
/* define message IDs */
#include <ioam/lib-trace/trace_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#define __plugin_msg_base trace_test_main.msg_id_base
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
/* define message IDs */
#include <ioam/lib-vxlan-gpe/vxlan_gpe_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#define __plugin_msg_base vxlan_gpe_test_main.msg_id_base
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
/* define message IDs */
#include <ioam/udp-ping/udp_ping_msg_enum.h>
#include <vlib/vlib.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vnet/ip/ip.h>
#include <vnet/ip/ip6_hop_by_hop.h>
#include <ioam/encap/ip6_ioam_trace.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#include <vnet/ip/ip.h>
#include <vlibapi/api.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#define vl_msg_id(n,h) n,
typedef enum {
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#include <lb/lb.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
/* define message IDs */
#include <memif/memif_msg_enum.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#include <vnet/ip/ip.h>
#include <memif/memif.h>
#include <nat/nat64.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <nat/nat_msg_enum.h>
#include <vnet/fib/fib_table.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#include <vnet/ip/ip.h>
#include <nat/nat.h>
#include <vppinfra/byte_order.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <pppoe/pppoe.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
+
#include <vppinfra/error.h>
#include <pppoe/pppoe.h>
bin_PROGRAMS += svmtool svmdbtool
-nobase_include_HEADERS += svm/svm.h svm/svm_common.h svm/ssvm.h svm/svmdb.h \
- svm/svm_fifo.h svm/svm_fifo_segment.h
+nobase_include_HEADERS += \
+ svm/memfd.h \
+ svm/ssvm.h \
+ svm/svm_common.h \
+ svm/svmdb.h \
+ svm/svm_fifo.h \
+ svm/svm_fifo_segment.h \
+ svm/svm.h
lib_LTLIBRARIES += libsvm.la libsvmdb.la
-libsvm_la_SOURCES = svm/svm.c svm/ssvm.c svm/svm_fifo.c svm/svm_fifo_segment.c
+libsvm_la_SOURCES = \
+ svm/svm.c \
+ svm/ssvm.c \
+ svm/svm_fifo.c \
+ svm/svm_fifo_segment.c \
+ svm/memfd.c
+
libsvm_la_LIBADD = libvppinfra.la -lrt -lpthread
libsvm_la_DEPENDENCIES = libvppinfra.la
--- /dev/null
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "memfd.h"
+
+int
+memfd_master_init (memfd_private_t * memfd, u32 master_index)
+{
+ int flags;
+ memfd_shared_header_t *sh;
+ u64 ticks = clib_cpu_time_now ();
+ u64 randomize_baseva;
+ void *oldheap;
+
+ if (memfd->memfd_size == 0)
+ return MEMFD_API_ERROR_NO_SIZE;
+
+ ASSERT (vec_c_string_is_terminated (memfd->name));
+ memfd->name = format (0, "memfd svm region %d", master_index);
+
+ memfd->fd = memfd_create ((char *) memfd->name, MFD_ALLOW_SEALING);
+ if (memfd->fd < 0)
+ {
+ clib_unix_warning ("create segment '%s'", memfd->name);
+ return MEMFD_API_ERROR_CREATE_FAILURE;
+ }
+
+ if ((ftruncate (memfd->fd, memfd->memfd_size)) == -1)
+ {
+ clib_unix_warning ("set memfd size");
+ return MEMFD_API_ERROR_SET_SIZE;
+ }
+
+ if ((fcntl (memfd->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
+ clib_unix_warning ("fcntl (F_ADD_SEALS, F_SEAL_SHRINK)");
+
+ flags = MAP_SHARED;
+ if (memfd->requested_va)
+ flags |= MAP_FIXED;
+
+ randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
+
+ if (memfd->requested_va)
+ memfd->requested_va += randomize_baseva;
+
+ sh = memfd->sh =
+ (memfd_shared_header_t *) mmap ((void *) memfd->requested_va,
+ memfd->memfd_size, PROT_READ | PROT_WRITE,
+ flags, memfd->fd, 0);
+
+ if (memfd->sh == MAP_FAILED)
+ {
+ clib_unix_warning ("mmap");
+ close (memfd->fd);
+ return MEMFD_API_ERROR_MMAP;
+ }
+
+ memfd->my_pid = getpid ();
+ sh->master_pid = memfd->my_pid;
+ sh->memfd_size = memfd->memfd_size;
+ sh->heap = mheap_alloc_with_flags
+ (((u8 *) sh) + MMAP_PAGESIZE, memfd->memfd_size - MMAP_PAGESIZE,
+ MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE);
+
+ sh->memfd_va = pointer_to_uword (sh);
+ sh->master_index = master_index;
+
+ oldheap = memfd_push_heap (sh);
+ sh->name = format (0, "%s%c", memfd->name, 0);
+ memfd_pop_heap (oldheap);
+
+ memfd->i_am_master = 1;
+
+ /* The application has to set set sh->ready... */
+ return 0;
+}
+
+/*
+ * Subtly different than svm_slave_init. The caller
+ * needs to acquire a usable file descriptor for the memfd segment
+ * e.g. via vppinfra/socket.c:default_socket_recvmsg
+ */
+
+int
+memfd_slave_init (memfd_private_t * memfd)
+{
+ memfd_shared_header_t *sh;
+
+ memfd->i_am_master = 0;
+
+ /* Map the segment once, to look at the shared header */
+ sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
+ memfd->fd, 0);
+ if (sh == MAP_FAILED)
+ {
+ clib_unix_warning ("slave research mmap");
+ close (memfd->fd);
+ return MEMFD_API_ERROR_MMAP;
+ }
+
+ memfd->requested_va = (u64) sh->memfd_va;
+ memfd->memfd_size = sh->memfd_size;
+ munmap (sh, MMAP_PAGESIZE);
+
+ sh = memfd->sh =
+ (void *) mmap ((void *) memfd->requested_va, memfd->memfd_size,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FIXED, memfd->fd, 0);
+
+ if (sh == MAP_FAILED)
+ {
+ clib_unix_warning ("slave final mmap");
+ close (memfd->fd);
+ return MEMFD_API_ERROR_MMAP;
+ }
+ sh->slave_pid = getpid ();
+ return 0;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
--- /dev/null
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __included_memfd_h__
+#define __included_memfd_h__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <time.h>
+#include <fcntl.h>
+#include <string.h>
+#include <vppinfra/clib.h>
+#include <vppinfra/vec.h>
+#include <vppinfra/hash.h>
+#include <vppinfra/bitmap.h>
+#include <vppinfra/fifo.h>
+#include <vppinfra/time.h>
+#include <vppinfra/mheap.h>
+#include <vppinfra/heap.h>
+#include <vppinfra/pool.h>
+#include <vppinfra/format.h>
+/* DGMS, memfd syscall not in glibc... */
+#include <vppinfra/linux/syscall.h>
+
+#ifndef MMAP_PAGESIZE
+#define MMAP_PAGESIZE (clib_mem_get_page_size())
+#endif
+
+#define MEMFD_N_OPAQUE 7
+
+typedef struct
+{
+ /* Spin-lock */
+ volatile u32 lock;
+ volatile u32 owner_pid;
+ int recursion_count;
+ u32 tag; /* for debugging */
+
+ /* The allocation arena */
+ void *heap;
+
+ /* Segment must be mapped at this address, or no supper */
+ u64 memfd_va;
+ /* The actual mmap size */
+ u64 memfd_size;
+ u32 master_pid;
+ u32 slave_pid;
+ u8 *name;
+ void *opaque[MEMFD_N_OPAQUE];
+
+ /* Set when the master application thinks it's time to make the donuts */
+ volatile u32 ready;
+
+ /* Needed to make unique MAC addresses, etc. */
+ u32 master_index;
+} memfd_shared_header_t;
+
+typedef struct
+{
+ memfd_shared_header_t *sh;
+ int fd;
+ u64 memfd_size;
+ u32 my_pid;
+ u32 vlib_hw_if_index;
+ uword requested_va;
+ int i_am_master;
+ u32 per_interface_next_index;
+ u32 *rx_queue;
+ u8 *name;
+} memfd_private_t;
+
+always_inline void
+memfd_lock (memfd_shared_header_t * h, u32 my_pid, u32 tag)
+{
+ if (h->owner_pid == my_pid)
+ {
+ h->recursion_count++;
+ return;
+ }
+
+ while (__sync_lock_test_and_set (&h->lock, 1))
+ ;
+
+ h->owner_pid = my_pid;
+ h->recursion_count = 1;
+ h->tag = tag;
+}
+
+always_inline void
+memfd_lock_non_recursive (memfd_shared_header_t * h, u32 tag)
+{
+ while (__sync_lock_test_and_set (&h->lock, 1))
+ ;
+
+ h->tag = tag;
+}
+
+always_inline void
+memfd_unlock (memfd_shared_header_t * h)
+{
+ if (--h->recursion_count == 0)
+ {
+ h->owner_pid = 0;
+ h->tag = 0;
+ CLIB_MEMORY_BARRIER ();
+ h->lock = 0;
+ }
+}
+
+always_inline void
+memfd_unlock_non_recursive (memfd_shared_header_t * h)
+{
+ h->tag = 0;
+ CLIB_MEMORY_BARRIER ();
+ h->lock = 0;
+}
+
+static inline void *
+memfd_push_heap (memfd_shared_header_t * sh)
+{
+ u8 *oldheap;
+ oldheap = clib_mem_set_heap (sh->heap);
+ return ((void *) oldheap);
+}
+
+static inline void
+memfd_pop_heap (void *oldheap)
+{
+ clib_mem_set_heap (oldheap);
+}
+
+#define foreach_memfd_api_error \
+_(NO_NAME, "No shared segment name", -100) \
+_(NO_SIZE, "Size not set (master)", -101) \
+_(CREATE_FAILURE, "Create failed", -102) \
+_(SET_SIZE, "Set size failed", -103) \
+_(MMAP, "mmap failed", -104) \
+_(SLAVE_TIMEOUT, "Slave map timeout", -105)
+
+typedef enum
+{
+#define _(n,s,c) MEMFD_API_ERROR_##n = c,
+ foreach_memfd_api_error
+#undef _
+} memfd_api_error_enum_t;
+
+#define MEMFD_API_ERROR_NO_NAME (-10)
+
+int memfd_master_init (memfd_private_t * memfd, u32 master_index);
+int memfd_slave_init (memfd_private_t * memfd);
+void memfd_delete (memfd_private_t * memfd);
+
+/* These do not belong here, but the original keeps running around... */
+/* $$$$ work w/ Damjan to fix properly */
+
+#ifndef F_LINUX_SPECIFIC_BASE
+#define F_LINUX_SPECIFIC_BASE 1024
+#endif
+#define MFD_ALLOW_SEALING 0x0002U
+#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
+#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
+
+#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
+#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
+#define F_SEAL_GROW 0x0004 /* prevent file from growing */
+#define F_SEAL_WRITE 0x0008 /* prevent writes */
+
+#endif /* __included_memfd_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
return (shm_name);
}
+void
+svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp)
+{
+ pthread_mutexattr_t attr;
+ pthread_condattr_t cattr;
+ int nbits, words, bit;
+ int overhead_space;
+ void *oldheap;
+ uword data_base;
+ ASSERT (rp);
+ int rv;
+
+ memset (rp, 0, sizeof (*rp));
+
+ if (pthread_mutexattr_init (&attr))
+ clib_unix_warning ("mutexattr_init");
+
+ if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
+ clib_unix_warning ("mutexattr_setpshared");
+
+ if (pthread_mutex_init (&rp->mutex, &attr))
+ clib_unix_warning ("mutex_init");
+
+ if (pthread_mutexattr_destroy (&attr))
+ clib_unix_warning ("mutexattr_destroy");
+
+ if (pthread_condattr_init (&cattr))
+ clib_unix_warning ("condattr_init");
+
+ if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
+ clib_unix_warning ("condattr_setpshared");
+
+ if (pthread_cond_init (&rp->condvar, &cattr))
+ clib_unix_warning ("cond_init");
+
+ if (pthread_condattr_destroy (&cattr))
+ clib_unix_warning ("condattr_destroy");
+
+ region_lock (rp, 1);
+
+ rp->virtual_base = a->baseva;
+ rp->virtual_size = a->size;
+
+ rp->region_heap =
+ mheap_alloc_with_flags (uword_to_pointer
+ (a->baseva + MMAP_PAGESIZE, void *),
+ (a->pvt_heap_size !=
+ 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
+ MHEAP_FLAG_DISABLE_VM);
+ oldheap = svm_push_pvt_heap (rp);
+
+ rp->region_name = (char *) format (0, "%s%c", a->name, 0);
+ vec_add1 (rp->client_pids, getpid ());
+
+ nbits = rp->virtual_size / MMAP_PAGESIZE;
+
+ ASSERT (nbits > 0);
+ rp->bitmap_size = nbits;
+ words = (nbits + BITS (uword) - 1) / BITS (uword);
+ vec_validate (rp->bitmap, words - 1);
+
+ overhead_space = MMAP_PAGESIZE /* header */ +
+ ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
+
+ bit = 0;
+ data_base = (uword) rp->virtual_base;
+
+ if (a->flags & SVM_FLAGS_NODATA)
+ rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
+
+ do
+ {
+ clib_bitmap_set_no_check (rp->bitmap, bit, 1);
+ bit++;
+ overhead_space -= MMAP_PAGESIZE;
+ data_base += MMAP_PAGESIZE;
+ }
+ while (overhead_space > 0);
+
+ rp->data_base = (void *) data_base;
+
+ /*
+ * Note: although the POSIX spec guarantees that only one
+ * process enters this block, we have to play games
+ * to hold off clients until e.g. the mutex is ready
+ */
+ rp->version = SVM_VERSION;
+
+ /* setup the data portion of the region */
+
+ rv = svm_data_region_create (a, rp);
+ if (rv)
+ {
+ clib_warning ("data_region_create: %d", rv);
+ }
+
+ region_unlock (rp);
+
+ svm_pop_heap (oldheap);
+}
+
/*
* svm_map_region
*/
{
int svm_fd;
svm_region_t *rp;
- pthread_mutexattr_t attr;
- pthread_condattr_t cattr;
int deadman = 0;
u8 junk = 0;
void *oldheap;
- int overhead_space;
int rv;
- uword data_base;
- int nbits, words, bit;
int pid_holding_region_lock;
u8 *shm_name;
int dead_region_recovery = 0;
return (0);
}
close (svm_fd);
- memset (rp, 0, sizeof (*rp));
-
- if (pthread_mutexattr_init (&attr))
- clib_unix_warning ("mutexattr_init");
-
- if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
- clib_unix_warning ("mutexattr_setpshared");
-
- if (pthread_mutex_init (&rp->mutex, &attr))
- clib_unix_warning ("mutex_init");
- if (pthread_mutexattr_destroy (&attr))
- clib_unix_warning ("mutexattr_destroy");
-
- if (pthread_condattr_init (&cattr))
- clib_unix_warning ("condattr_init");
-
- if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
- clib_unix_warning ("condattr_setpshared");
-
- if (pthread_cond_init (&rp->condvar, &cattr))
- clib_unix_warning ("cond_init");
-
- if (pthread_condattr_destroy (&cattr))
- clib_unix_warning ("condattr_destroy");
-
- region_lock (rp, 1);
-
- rp->virtual_base = a->baseva;
- rp->virtual_size = a->size;
-
- rp->region_heap =
- mheap_alloc_with_flags (uword_to_pointer
- (a->baseva + MMAP_PAGESIZE, void *),
- (a->pvt_heap_size !=
- 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE,
- MHEAP_FLAG_DISABLE_VM);
- oldheap = svm_push_pvt_heap (rp);
-
- rp->region_name = (char *) format (0, "%s%c", a->name, 0);
- vec_add1 (rp->client_pids, getpid ());
-
- nbits = rp->virtual_size / MMAP_PAGESIZE;
-
- ASSERT (nbits > 0);
- rp->bitmap_size = nbits;
- words = (nbits + BITS (uword) - 1) / BITS (uword);
- vec_validate (rp->bitmap, words - 1);
-
- overhead_space = MMAP_PAGESIZE /* header */ +
- ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE);
-
- bit = 0;
- data_base = (uword) rp->virtual_base;
-
- if (a->flags & SVM_FLAGS_NODATA)
- rp->flags |= SVM_FLAGS_NEED_DATA_INIT;
-
- do
- {
- clib_bitmap_set_no_check (rp->bitmap, bit, 1);
- bit++;
- overhead_space -= MMAP_PAGESIZE;
- data_base += MMAP_PAGESIZE;
- }
- while (overhead_space > 0);
-
- rp->data_base = (void *) data_base;
-
- /*
- * Note: although the POSIX spec guarantees that only one
- * process enters this block, we have to play games
- * to hold off clients until e.g. the mutex is ready
- */
- rp->version = SVM_VERSION;
-
- /* setup the data portion of the region */
-
- rv = svm_data_region_create (a, rp);
- if (rv)
- {
- clib_warning ("data_region_create: %d", rv);
- }
-
- region_unlock (rp);
-
- svm_pop_heap (oldheap);
+ svm_region_init_mapped_region (a, rp);
return ((void *) rp);
}
void *svm_region_find_or_create (svm_map_region_args_t * a);
void svm_region_init (void);
+void svm_region_init_mapped_region (svm_map_region_args_t * a,
+ svm_region_t * rp);
int svm_region_init_chroot (const char *root_path);
void svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid);
void svm_region_init_args (svm_map_region_args_t * a);
goto out;
}
- serialize_open_unix_file_descriptor (sm, fd);
+ serialize_open_clib_file_descriptor (sm, fd);
region_lock (client->db_rp, 20);
goto out;
}
- unserialize_open_unix_file_descriptor (sm, fd);
+ unserialize_open_clib_file_descriptor (sm, fd);
region_lock (client->db_rp, 21);
oldheap = svm_push_data_heap (client->db_rp);
goto file_done;
}
- unformat_init_unix_file (input, fd);
+ unformat_init_clib_file (input, fd);
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
*/
#include <vat/vat.h>
+#include <vppinfra/socket.h>
+#include <svm/memfd.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vnet/ip/ip.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2tp/l2tp.h>
#define __plugin_msg_base 0
#include <vlibapi/vat_helper_macros.h>
+#if VPP_API_TEST_BUILTIN == 0
+#include <netdb.h>
+
+u32
+vl (void *p)
+{
+ return vec_len (p);
+}
+
+int
+vat_socket_connect (vat_main_t * vam)
+{
+ return vl_socket_client_connect
+ (&vam->socket_client_main, (char *) vam->socket_name,
+ "vpp_api_test(s)", 0 /* default socket rx, tx buffer */ );
+}
+#else /* vpp built-in case, we don't do sockets... */
+int
+vat_socket_connect (vat_main_t * vam)
+{
+ return 0;
+}
+
+void
+vl_socket_client_read_reply (socket_client_main_t * scm)
+{
+};
+#endif
+
+
f64
vat_time_now (vat_main_t * vam)
{
{
vat_main_t *vam = &vat_main;
i32 retval = ntohl (mp->retval);
+ u32 length = ntohl (mp->length);
+
+ vec_reset_length (vam->cmd_reply);
vam->retval = retval;
- vam->cmd_reply = mp->reply;
+ if (retval == 0)
+ {
+ vec_validate (vam->cmd_reply, length);
+ clib_memcpy ((char *) (vam->cmd_reply), mp->reply, length);
+ vam->cmd_reply[length] = 0;
+ }
vam->result_ready = 1;
}
vat_main_t *vam = &vat_main;
vat_json_node_t node;
+ vec_reset_length (vam->cmd_reply);
+
vat_json_init_object (&node);
vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
vat_json_object_add_string_copy (&node, "reply", mp->reply);
vam->retval = retval;
vam->result_ready = 1;
}
+ vam->socket_client_main.control_pings_outstanding--;
}
static void vl_api_control_ping_reply_t_handler_json
vam->result_ready = 1;
}
+static clib_error_t *
+receive_fd_msg (int socket_fd, int *my_fd)
+{
+ char msgbuf[16];
+ char ctl[CMSG_SPACE (sizeof (int)) + CMSG_SPACE (sizeof (struct ucred))];
+ struct msghdr mh = { 0 };
+ struct iovec iov[1];
+ ssize_t size;
+ struct ucred *cr = 0;
+ struct cmsghdr *cmsg;
+ pid_t pid __attribute__ ((unused));
+ uid_t uid __attribute__ ((unused));
+ gid_t gid __attribute__ ((unused));
+
+ iov[0].iov_base = msgbuf;
+ iov[0].iov_len = 5;
+ mh.msg_iov = iov;
+ mh.msg_iovlen = 1;
+ mh.msg_control = ctl;
+ mh.msg_controllen = sizeof (ctl);
+
+ memset (ctl, 0, sizeof (ctl));
+
+ /* receive the incoming message */
+ size = recvmsg (socket_fd, &mh, 0);
+ if (size != 5)
+ {
+ return (size == 0) ? clib_error_return (0, "disconnected") :
+ clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
+ socket_fd);
+ }
+
+ cmsg = CMSG_FIRSTHDR (&mh);
+ while (cmsg)
+ {
+ if (cmsg->cmsg_level == SOL_SOCKET)
+ {
+ if (cmsg->cmsg_type == SCM_CREDENTIALS)
+ {
+ cr = (struct ucred *) CMSG_DATA (cmsg);
+ uid = cr->uid;
+ gid = cr->gid;
+ pid = cr->pid;
+ }
+ else if (cmsg->cmsg_type == SCM_RIGHTS)
+ {
+ clib_memcpy (my_fd, CMSG_DATA (cmsg), sizeof (int));
+ }
+ }
+ cmsg = CMSG_NXTHDR (&mh, cmsg);
+ }
+ return 0;
+}
+
+static void vl_api_memfd_segment_create_reply_t_handler
+ (vl_api_memfd_segment_create_reply_t * mp)
+{
+ /* Dont bother in the builtin version */
+#if VPP_API_TEST_BUILTIN == 0
+ vat_main_t *vam = &vat_main;
+ api_main_t *am = &api_main;
+ socket_client_main_t *scm = &vam->socket_client_main;
+ int my_fd = -1;
+ clib_error_t *error;
+ memfd_private_t memfd;
+ i32 retval = ntohl (mp->retval);
+
+ if (retval == 0)
+ {
+ error = receive_fd_msg (scm->socket_fd, &my_fd);
+ if (error)
+ {
+ retval = -99;
+ goto out;
+ }
+
+ memset (&memfd, 0, sizeof (memfd));
+ memfd.fd = my_fd;
+
+ vam->client_index_invalid = 1;
+
+ retval = memfd_slave_init (&memfd);
+ if (retval)
+ clib_warning ("WARNING: segment map returned %d", retval);
+
+ /* Pivot to the memory client segment that vpp just created */
+
+ am->vlib_rp = (void *) (memfd.requested_va + MMAP_PAGESIZE);
+
+ am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
+
+ vl_client_install_client_message_handlers ();
+
+ vl_client_connect_to_vlib_no_map ("pvt",
+ "vpp_api_test(p)",
+ 32 /* input_queue_length */ );
+ if (close (my_fd) < 0)
+ clib_unix_warning ("close memfd fd pivot");
+ vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
+
+ vl_socket_client_enable_disable (&vam->socket_client_main,
+ 0 /* disable socket */ );
+ }
+
+out:
+ if (vam->async_mode)
+ {
+ vam->async_errors += (retval < 0);
+ }
+ else
+ {
+ vam->retval = retval;
+ vam->result_ready = 1;
+ }
+#endif
+}
+
+static void vl_api_memfd_segment_create_reply_t_handler_json
+ (vl_api_memfd_segment_create_reply_t * mp)
+{
+ clib_warning ("no");
+}
+
+
static void vl_api_ip_address_details_t_handler
(vl_api_ip_address_details_t * mp)
{
_(VNET_IP4_FIB_COUNTERS, vnet_ip4_fib_counters) \
_(VNET_IP6_FIB_COUNTERS, vnet_ip6_fib_counters) \
_(VNET_IP4_NBR_COUNTERS, vnet_ip4_nbr_counters) \
-_(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters)
+_(VNET_IP6_NBR_COUNTERS, vnet_ip6_nbr_counters) \
+_(MEMFD_SEGMENT_CREATE_REPLY, memfd_segment_create_reply)
typedef struct
{
return 0;
}
-int
-exec (vat_main_t * vam)
-{
- api_main_t *am = &api_main;
- vl_api_cli_t *mp;
- f64 timeout;
- void *oldheap;
- u8 *cmd = 0;
- unformat_input_t *i = vam->input;
-
- if (vec_len (i->buffer) == 0)
- return -1;
-
- if (vam->exec_mode == 0 && unformat (i, "mode"))
- {
- vam->exec_mode = 1;
- return 0;
- }
- if (vam->exec_mode == 1 && (unformat (i, "exit") || unformat (i, "quit")))
- {
- vam->exec_mode = 0;
- return 0;
- }
-
-
- M (CLI, mp);
-
- /*
- * Copy cmd into shared memory.
- * In order for the CLI command to work, it
- * must be a vector ending in \n, not a C-string ending
- * in \n\0.
- */
- pthread_mutex_lock (&am->vlib_rp->mutex);
- oldheap = svm_push_data_heap (am->vlib_rp);
-
- vec_validate (cmd, vec_len (vam->input->buffer) - 1);
- clib_memcpy (cmd, vam->input->buffer, vec_len (vam->input->buffer));
-
- svm_pop_heap (oldheap);
- pthread_mutex_unlock (&am->vlib_rp->mutex);
-
- mp->cmd_in_shmem = pointer_to_uword (cmd);
- S (mp);
- timeout = vat_time_now (vam) + 10.0;
-
- while (vat_time_now (vam) < timeout)
- {
- if (vam->result_ready == 1)
- {
- u8 *free_me;
- if (vam->shmem_result != NULL)
- print (vam->ofp, "%s", vam->shmem_result);
- pthread_mutex_lock (&am->vlib_rp->mutex);
- oldheap = svm_push_data_heap (am->vlib_rp);
-
- free_me = (u8 *) vam->shmem_result;
- vec_free (free_me);
-
- svm_pop_heap (oldheap);
- pthread_mutex_unlock (&am->vlib_rp->mutex);
- return 0;
- }
- }
- return -99;
-}
-
/*
- * Future replacement of exec() that passes CLI buffers directly in
- * the API messages instead of an additional shared memory area.
+ * Pass CLI buffers directly in the CLI_INBAND API message,
+ * instead of an additional shared memory area.
*/
static int
exec_inband (vat_main_t * vam)
mp->length = htonl (len);
S (mp);
- W2 (ret, print (vam->ofp, "%s", vam->cmd_reply));
+ W (ret);
+ /* json responses may or may not include a useful reply... */
+ if (vec_len (vam->cmd_reply))
+ print (vam->ofp, (char *) (vam->cmd_reply));
return ret;
}
+int
+exec (vat_main_t * vam)
+{
+ return exec_inband (vam);
+}
+
static int
api_create_loopback (vat_main_t * vam)
{
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
/* Shut off async mode */
vam->async_mode = 0;
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
timeout = vat_time_now (vam) + 1.0;
/* Shut off async mode */
vam->async_mode = 0;
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
timeout = vat_time_now (vam) + 1.0;
/* Shut off async mode */
vam->async_mode = 0;
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
timeout = vat_time_now (vam) + 1.0;
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
/* send it... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
/* Wait for a reply... */
S (mp);
/* Use a control ping for synchronization */
- M (CONTROL_PING, mp_ping);
+ MPING (CONTROL_PING, mp_ping);
S (mp_ping);
W (ret);
return ret;
}
+static int
+api_memfd_segment_create (vat_main_t * vam)
+{
+ unformat_input_t *i = vam->input;
+ vl_api_memfd_segment_create_t *mp;
+ u64 size = 64 << 20;
+ int ret;
+
+#if VPP_API_TEST_BUILTIN == 1
+ errmsg ("memfd_segment_create (builtin) not supported");
+ return -99;
+#endif
+
+ while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (i, "size %U", unformat_memory_size, &size))
+ ;
+ else
+ break;
+ }
+
+ M (MEMFD_SEGMENT_CREATE, mp);
+ mp->requested_size = size;
+ S (mp);
+ W (ret);
+ return ret;
+}
+
static int
q_or_quit (vat_main_t * vam)
{
_(p2p_ethernet_del, "<intfc> | sw_if_index <nn> remote_mac <mac-address>") \
_(lldp_config, "system-name <name> tx-hold <nn> tx-interval <nn>") \
_(sw_interface_set_lldp, "<intfc> | sw_if_index <nn> [port-desc <description>] [disable]") \
-_(tcp_configure_src_addresses, "<ip4|6>first-<ip4|6>last [vrf <id>]")
+_(tcp_configure_src_addresses, "<ip4|6>first-<ip4|6>last [vrf <id>]") \
+_(memfd_segment_create,"size <nnn>")
/* List of command functions, CLI names map directly to functions */
#define foreach_cli_function \
vam->regenerate_interface_table = 0;
api_sw_interface_dump (vam);
}
+
+ /* Hack to pick up new client index after memfd_segment_create pivot */
+ if (vam->client_index_invalid)
+ {
+ vat_main_t *vam = &vat_main;
+ api_main_t *am = &api_main;
+
+ vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
+ vam->my_client_index = am->my_client_index;
+ vam->client_index_invalid = 0;
+ }
}
}
eval_current_line);
init_error_string_table (vam);
+ vec_validate (vam->cmd_reply, 0);
+ vec_reset_length (vam->cmd_reply);
unformat_init_command_line (a, argv);
interactive = 0;
else if (unformat (a, "json"))
json_output = 1;
+ else if (unformat (a, "socket-name %s", &vam->socket_name))
+ ;
+ else if (unformat (a, "default-socket"))
+ {
+ vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
+ }
else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
vec_add1 (vat_plugin_path, 0);
else if (unformat (a, "plugin_name_filter %s",
}
else
{
- fformat (stderr,
- "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n",
- argv[0]);
+ fformat
+ (stderr,
+ "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
+ "[plugin_path <path>][default-socket][socket-name <name>]\n"
+ "[plugin_name_filter <filter>][chroot prefix <path>]\n",
+ argv[0]);
exit (1);
}
}
setup_signal_handlers ();
- if (connect_to_vpe ("vpp_api_test") < 0)
+ if (vam->socket_name && vat_socket_connect (vam))
+ fformat (stderr, "WARNING: socket connection failed");
+
+ if (vam->socket_client_main.socket_fd == 0
+ && connect_to_vpe ("vpp_api_test") < 0)
{
svm_region_exit ();
fformat (stderr, "Couldn't connect to vpe, exiting...\n");
vam->json_output = json_output;
if (!json_output)
- {
- api_sw_interface_dump (vam);
- }
+ api_sw_interface_dump (vam);
vec_validate (vam->inbuf, 4096);
#ifndef __included_vat_h__
#define __included_vat_h__
+#define _GNU_SOURCE
#include <stdio.h>
#include <setjmp.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
#include <vppinfra/clib.h>
#include <vppinfra/format.h>
#include <vppinfra/error.h>
#include <vppinfra/time.h>
#include <vppinfra/macros.h>
+#include <vppinfra/socket.h>
#include <vnet/vnet.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
/* Errors by number */
uword *error_string_by_error_number;
-
/* Main thread can spin (w/ timeout) here if needed */
u32 async_mode;
u32 async_errors;
volatile i32 retval;
volatile u32 sw_if_index;
volatile u8 *shmem_result;
- volatile u8 *cmd_reply;
+ u8 *cmd_reply;
/* our client index */
u32 my_client_index;
+ int client_index_invalid;
/* Time is of the essence... */
clib_time_t clib_time;
ip4_nbr_counter_t **ip4_nbr_counters;
ip6_nbr_counter_t **ip6_nbr_counters;
+ socket_client_main_t socket_client_main;
+ u8 *socket_name;
+
/* Convenience */
vlib_main_t *vlib_main;
} vat_main_t;
u8 *format_ip46_address (u8 * s, va_list * args);
u8 *format_ethernet_address (u8 * s, va_list * args);
+int vat_socket_connect (vat_main_t * vam);
+
#if VPP_API_TEST_BUILTIN
#define print api_cli_output
void api_cli_output (void *, const char *fmt, ...);
# See the License for the specific language governing permissions and
# limitations under the License.
-lib_LTLIBRARIES += libvlibmemory.la libvlibmemoryclient.la \
- libvlibsocket.la
+lib_LTLIBRARIES += libvlibmemory.la libvlibmemoryclient.la
libvlibmemory_la_DEPENDENCIES = libvppinfra.la libsvm.la libvlib.la
libvlibmemory_la_LIBADD = $(libvlibmemory_la_DEPENDENCIES) -lpthread
vlibmemory/memclnt.api \
vlibmemory/memory_shared.c \
vlibmemory/memory_vlib.c \
+ vlibmemory/socksvr_vlib.c \
vlibmemory/unix_shared_memory_queue.c \
vlibmemory/unix_shared_memory_queue.h \
vlibmemory/vl_memory_api_h.h \
vlibmemory/memclnt.api \
vlibmemory/memory_client.c \
vlibmemory/memory_shared.c \
+ vlibmemory/socket_client.c \
vlibmemory/unix_shared_memory_queue.c \
vlibmemory/unix_shared_memory_queue.h \
vlibmemory/vl_memory_api_h.h \
vlibmemory/unix_shared_memory_queue.h \
vlibmemory/memclnt.api.h
-libvlibsocket_la_DEPENDENCIES = libvppinfra.la libvlib.la libvlibmemory.la
-libvlibsocket_la_LIBADD = $(libvlibsocket_la_DEPENDENCIES)
-libvlibsocket_la_SOURCES = \
- vlibsocket/api.h \
- vlibsocket/sockclnt.api \
- vlibsocket/sockclnt_vlib.c \
- vlibsocket/socksvr_vlib.c \
- vlibsocket/vl_socket_api_h.h \
- vlibsocket/vl_socket_msg_enum.h
-
-nobase_include_HEADERS += \
- vlibsocket/api.h \
- vlibsocket/vl_socket_api_h.h \
- vlibsocket/vl_socket_msg_enum.h \
- vlibsocket/sockclnt.api.h
-
BUILT_SOURCES += \
- vlibsocket/sockclnt.api.h \
vlibmemory/memclnt.api.h \
vlibmemory/memclnt.api.json
-API_FILES += vlibmemory/memclnt.api
+noinst_PROGRAMS += sock_test
+
+sock_test_SOURCES = vlibsocket/sock_test.c
+
+API_FILES += vlibmemory/memclnt.api
# vi:syntax=automake
}
}
- unformat_init_unix_file (&sub_input, fd);
+ unformat_init_clib_file (&sub_input, fd);
vlib_cli_input (vm, &sub_input, 0, 0);
unformat_free (&sub_input);
static linux_epoll_main_t linux_epoll_main;
static void
-linux_epoll_file_update (clib_file_t * f, unix_file_update_type_t update_type)
+linux_epoll_file_update (clib_file_t * f, clib_file_update_type_t update_type)
{
clib_file_main_t *fm = &file_main;
linux_epoll_main_t *em = &linux_epoll_main;
u8 *name; /**< Client name */
+ /* Zombie apocalypse checking */
+ f64 last_heard;
+ int last_queue_head;
+ int unanswered_pings;
+
/** shared memory only: pointer to client input queue */
unix_shared_memory_queue_t *vl_input_queue;
+ svm_region_t *vlib_rp;
+ void *shmem_hdr;
/* socket server and client */
u32 clib_file_index; /**< Socket only: file index */
i8 *unprocessed_input; /**< Socket only: pending input */
u32 unprocessed_msg_length; /**< Socket only: unprocssed length */
u8 *output_vector; /**< Socket only: output vecto */
+ int *additional_fds_to_close;
/* socket client only */
u32 server_handle; /**< Socket client only: server handle */
svm_region_t *vlib_rp;
/** Vector of all mapped shared-VM segments */
+ svm_region_t **vlib_private_rps;
svm_region_t **mapped_shmem_regions;
/** Binary API shared-memory segment header pointer */
#define REPLY_MSG_ID_BASE 0
#endif
-#define REPLY_MACRO(t) \
-do { \
- unix_shared_memory_queue_t * q; \
- rv = vl_msg_api_pd_handler (mp, rv); \
- q = vl_api_client_index_to_input_queue (mp->client_index); \
- if (!q) \
- return; \
- \
- rmp = vl_msg_api_alloc (sizeof (*rmp)); \
- rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
- rmp->context = mp->context; \
- rmp->retval = ntohl(rv); \
- \
- vl_msg_api_send_shmem (q, (u8 *)&rmp); \
+#define REPLY_MACRO(t) \
+do { \
+ vl_api_registration_t *rp; \
+ rv = vl_msg_api_pd_handler (mp, rv); \
+ rp = vl_api_client_index_to_registration (mp->client_index); \
+ if (rp == 0) \
+ return; \
+ \
+ rmp = vl_msg_api_alloc (sizeof (*rmp)); \
+ rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
+ rmp->context = mp->context; \
+ rmp->retval = ntohl(rv); \
+ \
+ vl_msg_api_send (rp, (u8 *)rmp); \
} while(0);
-#define REPLY_MACRO2(t, body) \
-do { \
- unix_shared_memory_queue_t * q; \
- rv = vl_msg_api_pd_handler (mp, rv); \
- q = vl_api_client_index_to_input_queue (mp->client_index); \
- if (!q) \
- return; \
- \
- rmp = vl_msg_api_alloc (sizeof (*rmp)); \
- rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
- rmp->context = mp->context; \
- rmp->retval = ntohl(rv); \
- do {body;} while (0); \
- vl_msg_api_send_shmem (q, (u8 *)&rmp); \
+#define REPLY_MACRO2(t, body) \
+do { \
+ vl_api_registration_t *rp; \
+ rv = vl_msg_api_pd_handler (mp, rv); \
+ rp = vl_api_client_index_to_registration (mp->client_index); \
+ if (rp == 0) \
+ return; \
+ \
+ rmp = vl_msg_api_alloc (sizeof (*rmp)); \
+ rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
+ rmp->context = mp->context; \
+ rmp->retval = ntohl(rv); \
+ do {body;} while (0); \
+ vl_msg_api_send (rp, (u8 *)rmp); \
} while(0);
-#define REPLY_MACRO3(t, n, body) \
-do { \
- unix_shared_memory_queue_t * q; \
- rv = vl_msg_api_pd_handler (mp, rv); \
- q = vl_api_client_index_to_input_queue (mp->client_index); \
- if (!q) \
- return; \
- \
- rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
- rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
- rmp->context = mp->context; \
- rmp->retval = ntohl(rv); \
- do {body;} while (0); \
- vl_msg_api_send_shmem (q, (u8 *)&rmp); \
+#define REPLY_MACRO3(t, n, body) \
+do { \
+ vl_api_registration_t *rp; \
+ rv = vl_msg_api_pd_handler (mp, rv); \
+ rp = vl_api_client_index_to_registration (mp->client_index); \
+ if (rp == 0) \
+ return; \
+ \
+ rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
+ rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
+ rmp->context = mp->context; \
+ rmp->retval = ntohl(rv); \
+ do {body;} while (0); \
+ vl_msg_api_send (rp, (u8 *)rmp); \
} while(0);
-#define REPLY_MACRO4(t, n, body) \
-do { \
- unix_shared_memory_queue_t * q; \
- u8 is_error = 0; \
- rv = vl_msg_api_pd_handler (mp, rv); \
- q = vl_api_client_index_to_input_queue (mp->client_index); \
- if (!q) \
- return; \
- \
- rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
- if (!rmp) \
- { \
- /* if there isn't enough memory, try to allocate */ \
- /* some at least for returning an error */ \
- rmp = vl_msg_api_alloc (sizeof (*rmp)); \
- if (!rmp) \
- return; \
- \
- memset (rmp, 0, sizeof (*rmp)); \
- rv = VNET_API_ERROR_TABLE_TOO_BIG; \
- is_error = 1; \
- } \
- rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
- rmp->context = mp->context; \
- rmp->retval = ntohl(rv); \
- if (!is_error) \
- do {body;} while (0); \
- vl_msg_api_send_shmem (q, (u8 *)&rmp); \
+#define REPLY_MACRO4(t, n, body) \
+do { \
+ vl_api_registration_t *rp; \
+ u8 is_error = 0; \
+ rv = vl_msg_api_pd_handler (mp, rv); \
+ \
+ rp = vl_api_client_index_to_registration (mp->client_index); \
+ if (rp == 0) \
+ return; \
+ \
+ rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
+ if (!rmp) \
+ { \
+ /* if there isn't enough memory, try to allocate */ \
+ /* some at least for returning an error */ \
+ rmp = vl_msg_api_alloc (sizeof (*rmp)); \
+ if (!rmp) \
+ return; \
+ \
+ memset (rmp, 0, sizeof (*rmp)); \
+ rv = VNET_API_ERROR_TABLE_TOO_BIG; \
+ is_error = 1; \
+ } \
+ rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
+ rmp->context = mp->context; \
+ rmp->retval = ntohl(rv); \
+ if (!is_error) \
+ do {body;} while (0); \
+ vl_msg_api_send (rp, (u8 *)rmp); \
} while(0);
/* "trust, but verify" */
/* M: construct, but don't yet send a message */
#define M(T, mp) \
do { \
+ socket_client_main_t *scm = &vam->socket_client_main; \
vam->result_ready = 0; \
- mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \
+ if (scm->socket_enable) \
+ { \
+ mp = (void *)scm->socket_tx_buffer; \
+ scm->socket_tx_nbytes = sizeof (*mp); \
+ } \
+ else \
+ mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \
memset (mp, 0, sizeof (*mp)); \
mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
mp->client_index = vam->my_client_index; \
} while(0);
+/* MPING: construct a control-ping message, don't send it yet */
+#define MPING(T, mp) \
+do { \
+ socket_client_main_t *scm = &vam->socket_client_main; \
+ vam->result_ready = 0; \
+ if (scm->socket_enable) \
+ { \
+ mp = (void *)scm->socket_tx_buffer; \
+ scm->socket_tx_nbytes = sizeof (*mp); \
+ } \
+ else \
+ mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \
+ memset (mp, 0, sizeof (*mp)); \
+ mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
+ mp->client_index = vam->my_client_index; \
+ scm->control_pings_outstanding++; \
+} while(0);
+
#define M2(T, mp, n) \
do { \
+ socket_client_main_t *scm = &vam->socket_client_main; \
vam->result_ready = 0; \
- mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)+(n)); \
+ if (scm->socket_enable) \
+ { \
+ mp = (void *)scm->socket_tx_buffer; \
+ scm->socket_tx_nbytes = sizeof (*mp) + n; \
+ } \
+ else \
+ mp = vl_msg_api_alloc_as_if_client(sizeof(*mp) + n); \
memset (mp, 0, sizeof (*mp)); \
mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
mp->client_index = vam->my_client_index; \
} while(0);
/* S: send a message */
-#define S(mp) (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
+#define S(mp) \
+do { \
+ int n; \
+ socket_client_main_t *scm = &vam->socket_client_main; \
+ if (scm->socket_enable) \
+ { \
+ msgbuf_t msgbuf; \
+ \
+ msgbuf.q = 0; \
+ msgbuf.gc_mark_timestamp = 0; \
+ msgbuf.data_len = ntohl(scm->socket_tx_nbytes); \
+ \
+ n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf)); \
+ if (n < sizeof (msgbuf)) \
+ clib_unix_warning ("socket write (msgbuf)"); \
+ \
+ n = write (scm->socket_fd, scm->socket_tx_buffer, \
+ scm->socket_tx_nbytes); \
+ if (n < scm->socket_tx_nbytes) \
+ clib_unix_warning ("socket write (msg)"); \
+ } \
+ else \
+ vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp); \
+ } while (0);
/* W: wait for results, with timeout */
-#define W(ret) \
-do { \
- f64 timeout = vat_time_now (vam) + 1.0; \
- ret = -99; \
- \
- while (vat_time_now (vam) < timeout) { \
- if (vam->result_ready == 1) { \
- ret = vam->retval; \
- break; \
- } \
- vat_suspend (vam->vlib_main, 1e-5); \
- } \
+#define W(ret) \
+do { \
+ f64 timeout = vat_time_now (vam) + 1.0; \
+ socket_client_main_t *scm = &vam->socket_client_main; \
+ ret = -99; \
+ \
+ vl_socket_client_read_reply (scm); \
+ while (vat_time_now (vam) < timeout) { \
+ if (vam->result_ready == 1) { \
+ ret = vam->retval; \
+ break; \
+ } \
+ vat_suspend (vam->vlib_main, 1e-5); \
+ } \
} while(0);
/* W2: wait for results, with timeout */
-#define W2(ret, body) \
-do { \
- f64 timeout = vat_time_now (vam) + 1.0; \
- ret = -99; \
- \
- while (vat_time_now (vam) < timeout) { \
- if (vam->result_ready == 1) { \
- (body); \
- ret = vam->retval; \
- break; \
- } \
- vat_suspend (vam->vlib_main, 1e-5); \
- } \
+#define W2(ret, body) \
+do { \
+ f64 timeout = vat_time_now (vam) + 1.0; \
+ socket_client_main_t *scm = &vam->socket_client_main; \
+ ret = -99; \
+ \
+ vl_socket_client_read_reply (scm); \
+ while (vat_time_now (vam) < timeout) { \
+ if (vam->result_ready == 1) { \
+ (body); \
+ ret = vam->retval; \
+ break; \
+ } \
+ vat_suspend (vam->vlib_main, 1e-5); \
+ } \
} while(0);
}
void vl_enable_disable_memory_api (vlib_main_t * vm, int yesno);
+
#endif /* included_vlibmemory_api_h */
/*
#define included_vlibmemory_api_common_h
#include <svm/svm_common.h>
+#include <vppinfra/file.h>
#include <vlibapi/api_common.h>
#include <vlibmemory/unix_shared_memory_queue.h>
/*
* Initializers for the (shared-memory) rings
- * _(size, n). Note: each msg has an 8 byte header.
- * Might want to change that to an index sometime.
+ * _(size, n). Note: each msg has space for a header.
*/
#define foreach_vl_aring_size \
-_(64+8, 1024) \
-_(256+8, 128) \
-_(1024+8, 64)
+_(64+sizeof(ring_alloc_t), 1024) \
+_(256+sizeof(ring_alloc_t), 128) \
+_(1024+sizeof(ring_alloc_t), 64)
#define foreach_clnt_aring_size \
-_(1024+8, 1024) \
-_(2048+8, 128) \
-_(4096+8, 8)
+ _(1024+sizeof(ring_alloc_t), 1024) \
+ _(2048+sizeof(ring_alloc_t), 128) \
+ _(4096+sizeof(ring_alloc_t), 8)
typedef struct vl_shmem_hdr_
{
/* Number of garbage-collected messages */
u32 garbage_collects;
-
} vl_shmem_hdr_t;
#define VL_SHM_VERSION 2
int vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
const char *client_name,
int rx_queue_size);
+int vl_client_connect_to_vlib_no_map (const char *svm_name,
+ const char *client_name,
+ int rx_queue_size);
u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
u32 vl_api_memclnt_create_internal (char *, unix_shared_memory_queue_t *);
+void vl_init_shmem (svm_region_t * vlib_rp, int is_vlib,
+ int is_private_region);
+void vl_client_install_client_message_handlers (void);
+
+/* API messages over sockets */
+
+extern vlib_node_registration_t memclnt_node;
+extern volatile int **vl_api_queue_cursizes;
+
+/* Events sent to the memclnt process */
+#define QUEUE_SIGNAL_EVENT 1
+#define SOCKET_READ_EVENT 2
+
+#define API_SOCKET_FILE "/run/vpp-api.sock"
+
+typedef struct
+{
+ clib_file_t *clib_file;
+ vl_api_registration_t *regp;
+ u8 *data;
+} vl_socket_args_for_process_t;
+
+typedef struct
+{
+ /* Server port number */
+ u8 *socket_name;
+
+ /* By default, localhost... */
+ u32 bind_address;
+
+ /*
+ * (listen, server, client) registrations. Shared memory
+ * registrations are in shared memory
+ */
+ vl_api_registration_t *registration_pool;
+ /*
+ * Chain-drag variables, so message API handlers
+ * (generally) don't know whether they're talking to a socket
+ * or to a shared-memory connection.
+ */
+ vl_api_registration_t *current_rp;
+ clib_file_t *current_uf;
+ /* One input buffer, shared across all sockets */
+ i8 *input_buffer;
+
+ /* pool of process args for socket clients */
+ vl_socket_args_for_process_t *process_args;
+
+ /* Listen for API connections here */
+ clib_socket_t socksvr_listen_socket;
+} socket_main_t;
+
+extern socket_main_t socket_main;
+
+typedef struct
+{
+ int socket_fd;
+ /* Temporarily disable the connection, so we can keep it around... */
+ int socket_enable;
+
+ clib_socket_t client_socket;
+
+ u32 socket_buffer_size;
+ u8 *socket_tx_buffer;
+ u8 *socket_rx_buffer;
+ u32 socket_tx_nbytes;
+ int control_pings_outstanding;
+} socket_client_main_t;
+
+extern socket_client_main_t socket_client_main;
+
+#define SOCKET_CLIENT_DEFAULT_BUFFER_SIZE 4096
+
+void socksvr_add_pending_output (struct clib_file *uf,
+ struct vl_api_registration_ *cf,
+ u8 * buffer, uword buffer_bytes);
+
+void vl_free_socket_registration_index (u32 pool_index);
+void vl_socket_process_msg (struct clib_file *uf,
+ struct vl_api_registration_ *rp, i8 * input_v);
+clib_error_t *vl_socket_read_ready (struct clib_file *uf);
+void vl_socket_add_pending_output (struct clib_file *uf,
+ struct vl_api_registration_ *rp,
+ u8 * buffer, uword buffer_bytes);
+void vl_socket_add_pending_output_no_flush (struct clib_file *uf,
+ struct vl_api_registration_ *rp,
+ u8 * buffer, uword buffer_bytes);
+clib_error_t *vl_socket_write_ready (struct clib_file *uf);
+void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
+u32 sockclnt_open_index (char *client_name, char *hostname, int port);
+void sockclnt_close_index (u32 index);
+void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
+vl_api_registration_t *sockclnt_get_registration (u32 index);
+void vl_api_socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
+ i8 * input_v);
+
+int
+vl_socket_client_connect (socket_client_main_t * scm, char *socket_path,
+ char *client_name, u32 socket_buffer_size);
+void vl_socket_client_read_reply (socket_client_main_t * scm);
+void vl_socket_client_enable_disable (socket_client_main_t * scm, int enable);
#endif /* included_vlibmemory_api_common_h */
u16 first_msg_id;
u16 last_msg_id;
};
+
+/*
+ * Create a socket client registration.
+ */
+define sockclnt_create {
+ u8 name[64]; /* for show, find by name, whatever */
+ u32 context; /* opaque value to be returned in the reply */
+};
+
+define sockclnt_create_reply {
+ i32 response; /* Non-negative = success */
+ u64 handle; /* handle by which vlib knows this client */
+ u32 index; /* index, used e.g. by API trace replay */
+ u32 context; /* opaque value from the create request */
+};
+
+/*
+ * Delete a client registration
+ */
+define sockclnt_delete {
+ u32 index; /* index, used e.g. by API trace replay */
+ u64 handle; /* handle by which vlib knows this client */
+};
+
+define sockclnt_delete_reply {
+ i32 response; /* Non-negative = success */
+ u64 handle; /* in case the client wonders */
+};
+
+/*
+ * Ask vpp for a memfd shared segment
+ */
+define memfd_segment_create {
+ u32 client_index;
+ u32 context;
+ u64 requested_size;
+};
+
+/*
+ * Reply
+ */
+define memfd_segment_create_reply
+{
+ u32 context;
+ i32 retval;
+ u32 master_fd;
+};
+
+/*
+ * Memory client ping / response
+ * Only sent on inactive connections
+ */
+autoreply define memclnt_keepalive
+{
+ u32 client_index;
+ u32 context;
+};
}
}
+/**
+ * Stave off the binary API dead client reaper
+ * Only sent to inactive clients
+ */
+static void
+vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
+{
+ vl_api_memclnt_keepalive_reply_t *rmp;
+ api_main_t *am;
+ vl_shmem_hdr_t *shmem_hdr;
+
+ am = &api_main;
+ shmem_hdr = am->shmem_hdr;
+
+ rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
+ memset (rmp, 0, sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
+ rmp->context = mp->context;
+ vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
+}
+
#define foreach_api_msg \
_(RX_THREAD_EXIT, rx_thread_exit) \
_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
-_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
+_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
+_(MEMCLNT_KEEPALIVE, memclnt_keepalive)
-int
-vl_client_api_map (const char *region_name)
+void
+vl_client_install_client_message_handlers (void)
{
- int rv;
-
- if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
- {
- return rv;
- }
#define _(N,n) \
vl_msg_api_set_handlers(VL_API_##N, #n, \
sizeof(vl_api_##n##_t), 1);
foreach_api_msg;
#undef _
+}
+
+
+int
+vl_client_api_map (const char *region_name)
+{
+ int rv;
+
+ if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
+ return rv;
+
+ vl_client_install_client_message_handlers ();
return 0;
}
static int
connect_to_vlib_internal (const char *svm_name,
const char *client_name,
- int rx_queue_size, int want_pthread)
+ int rx_queue_size, int want_pthread, int do_map)
{
int rv = 0;
memory_client_main_t *mm = &memory_client_main;
- if ((rv = vl_client_api_map (svm_name)))
+ if (do_map && (rv = vl_client_api_map (svm_name)))
{
clib_warning ("vl_client_api map rv %d", rv);
return rv;
const char *client_name, int rx_queue_size)
{
return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
- 1 /* want pthread */ );
+ 1 /* want pthread */ ,
+ 1 /* do map */ );
}
int
int rx_queue_size)
{
return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
- 0 /* want pthread */ );
+ 0 /* want pthread */ ,
+ 1 /* do map */ );
+}
+
+int
+vl_client_connect_to_vlib_no_map (const char *svm_name,
+ const char *client_name, int rx_queue_size)
+{
+ return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
+ 1 /* want pthread */ ,
+ 0 /* dont map */ );
}
void
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_typedefs
+socket_main_t socket_main;
+
+#define DEBUG_MESSAGE_BUFFER_OVERRUN 0
+
static inline void *
vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
{
shmem_hdr = am->shmem_hdr;
+#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
+ nbytes += 4;
+#endif
+
if (shmem_hdr == 0)
{
clib_warning ("shared memory header NULL");
pthread_mutex_unlock (&am->vlib_rp->mutex);
out:
+#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
+ {
+ nbytes -= 4;
+ u32 *overrun;
+ overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
+ *overrun = 0x1badbabe;
+ }
+#endif
rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
+
return (rv->data);
}
{
rv->q = 0;
rv->gc_mark_timestamp = 0;
+#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
+ {
+ u32 *overrun;
+ overrun = (u32 *) (rv->data + ntohl (rv->data_len));
+ ASSERT (*overrun == 0x1badbabe);
+ }
+#endif
return;
}
pthread_mutex_lock (&am->vlib_rp->mutex);
oldheap = svm_push_data_heap (am->vlib_rp);
+
+#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
+ {
+ u32 *overrun;
+ overrun = (u32 *) (rv->data + ntohl (rv->data_len));
+ ASSERT (*overrun == 0x1badbabe);
+ }
+#endif
+
clib_mem_free (rv);
svm_pop_heap (oldheap);
pthread_mutex_unlock (&am->vlib_rp->mutex);
am->api_pvt_heap_size = size;
}
+void
+vl_init_shmem (svm_region_t * vlib_rp, int is_vlib, int is_private_region)
+{
+ api_main_t *am = &api_main;
+ vl_shmem_hdr_t *shmem_hdr = 0;
+ u32 vlib_input_queue_length;
+ void *oldheap;
+ ASSERT (vlib_rp);
+
+ /* $$$$ need private region config parameters */
+
+ oldheap = svm_push_data_heap (vlib_rp);
+
+ vec_validate (shmem_hdr, 0);
+ shmem_hdr->version = VL_SHM_VERSION;
+
+ /* vlib main input queue */
+ vlib_input_queue_length = 1024;
+ if (am->vlib_input_queue_length)
+ vlib_input_queue_length = am->vlib_input_queue_length;
+
+ shmem_hdr->vl_input_queue =
+ unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
+ getpid (), am->vlib_signal);
+
+ /* Set up the msg ring allocator */
+#define _(sz,n) \
+ do { \
+ ring_alloc_t _rp; \
+ _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
+ _rp.size = (sz); \
+ _rp.nitems = n; \
+ _rp.hits = 0; \
+ _rp.misses = 0; \
+ vec_add1(shmem_hdr->vl_rings, _rp); \
+ } while (0);
+
+ foreach_vl_aring_size;
+#undef _
+
+#define _(sz,n) \
+ do { \
+ ring_alloc_t _rp; \
+ _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
+ _rp.size = (sz); \
+ _rp.nitems = n; \
+ _rp.hits = 0; \
+ _rp.misses = 0; \
+ vec_add1(shmem_hdr->client_rings, _rp); \
+ } while (0);
+
+ foreach_clnt_aring_size;
+#undef _
+
+ if (is_private_region == 0)
+ {
+ am->shmem_hdr = shmem_hdr;
+ am->vlib_rp = vlib_rp;
+ am->our_pid = getpid ();
+ if (is_vlib)
+ am->shmem_hdr->vl_pid = am->our_pid;
+ }
+ else
+ shmem_hdr->vl_pid = am->our_pid;
+
+ svm_pop_heap (oldheap);
+
+ /*
+ * After absolutely everything that a client might see is set up,
+ * declare the shmem region valid
+ */
+ vlib_rp->user_ctx = shmem_hdr;
+
+ pthread_mutex_unlock (&vlib_rp->mutex);
+}
+
+
int
vl_map_shmem (const char *region_name, int is_vlib)
{
svm_map_region_args_t _a, *a = &_a;
svm_region_t *vlib_rp, *root_rp;
- void *oldheap;
- vl_shmem_hdr_t *shmem_hdr = 0;
api_main_t *am = &api_main;
int i, rv;
struct timespec ts, tsrem;
- u32 vlib_input_queue_length;
char *vpe_api_region_suffix = "-vpe-api";
memset (a, 0, sizeof (*a));
}
/* Nope, it's our problem... */
+ vl_init_shmem (vlib_rp, 1 /* is vlib */ , 0 /* is_private_region */ );
- oldheap = svm_push_data_heap (vlib_rp);
-
- vec_validate (shmem_hdr, 0);
- shmem_hdr->version = VL_SHM_VERSION;
-
- /* vlib main input queue */
- vlib_input_queue_length = 1024;
- if (am->vlib_input_queue_length)
- vlib_input_queue_length = am->vlib_input_queue_length;
-
- shmem_hdr->vl_input_queue =
- unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
- getpid (), am->vlib_signal);
-
- /* Set up the msg ring allocator */
-#define _(sz,n) \
- do { \
- ring_alloc_t _rp; \
- _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
- _rp.size = (sz); \
- _rp.nitems = n; \
- _rp.hits = 0; \
- _rp.misses = 0; \
- vec_add1(shmem_hdr->vl_rings, _rp); \
- } while (0);
-
- foreach_vl_aring_size;
-#undef _
-
-#define _(sz,n) \
- do { \
- ring_alloc_t _rp; \
- _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
- _rp.size = (sz); \
- _rp.nitems = n; \
- _rp.hits = 0; \
- _rp.misses = 0; \
- vec_add1(shmem_hdr->client_rings, _rp); \
- } while (0);
-
- foreach_clnt_aring_size;
-#undef _
-
- am->shmem_hdr = shmem_hdr;
- am->vlib_rp = vlib_rp;
- am->our_pid = getpid ();
- if (is_vlib)
- am->shmem_hdr->vl_pid = am->our_pid;
-
- svm_pop_heap (oldheap);
-
- /*
- * After absolutely everything that a client might see is set up,
- * declare the shmem region valid
- */
- vlib_rp->user_ctx = shmem_hdr;
-
- pthread_mutex_unlock (&vlib_rp->mutex);
vec_add1 (am->mapped_shmem_regions, vlib_rp);
return 0;
}
vl_api_registration_t *
vl_api_client_index_to_registration (u32 index)
{
+ if (PREDICT_FALSE (socket_main.current_rp != 0))
+ return socket_main.current_rp;
+
return (vl_api_client_index_to_registration_internal (index));
}
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_endianfun
-void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
- __attribute__ ((weak));
-
-void
-vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
-{
- static int count;
-
- if (count++ < 5)
- clib_warning ("need to link against -lvlibsocket, msg not sent!");
-}
+extern void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
void
vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
}
else
{
- vl_msg_api_send_shmem (rp->vl_input_queue, elem);
+ vl_msg_api_send_shmem (rp->vl_input_queue, (u8 *) & elem);
}
}
int rv = 0;
void *oldheap;
api_main_t *am = &api_main;
+ u8 *serialized_message_table_in_shmem;
/*
* This is tortured. Maintain a vlib-address-space private
memset (regp, 0, sizeof (*regp));
regp->registration_type = REGISTRATION_TYPE_SHMEM;
regp->vl_api_registration_pool_index = regpp - am->vl_clients;
+ regp->vlib_rp = svm;
+ regp->shmem_hdr = am->shmem_hdr;
q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
mp->input_queue;
regp->name = format (0, "%s", mp->name);
vec_add1 (regp->name, 0);
+ serialized_message_table_in_shmem = vl_api_serialize_message_table (am, 0);
+
pthread_mutex_unlock (&svm->mutex);
svm_pop_heap (oldheap);
- ASSERT (am->serialized_message_table_in_shmem);
-
rp = vl_msg_api_alloc (sizeof (*rp));
rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
rp->handle = (uword) regp;
am->shmem_hdr->application_restarts);
rp->context = mp->context;
rp->response = ntohl (rv);
- rp->message_table =
- pointer_to_uword (am->serialized_message_table_in_shmem);
+ rp->message_table = pointer_to_uword (serialized_message_table_in_shmem);
vl_msg_api_send_shmem (q, (u8 *) & rp);
}
if (!pool_is_free (am->vl_clients, regpp))
{
+ int i;
regp = *regpp;
svm = am->vlib_rp;
+ int private_registration = 0;
- /* $$$ check the input queue for e.g. punted sf's */
-
+ /*
+ * Note: the API message handling path will set am->vlib_rp
+ * as appropriate for pairwise / private memory segments
+ */
rp = vl_msg_api_alloc (sizeof (*rp));
rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
rp->handle = mp->handle;
return;
}
+ /* For horizontal scaling, add a hash table... */
+ for (i = 0; i < vec_len (am->vlib_private_rps); i++)
+ {
+ /* Is this a pairwise / private API segment? */
+ if (am->vlib_private_rps[i] == svm)
+ {
+ /* Note: account for the memfd header page */
+ u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
+ u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
+
+ /*
+ * Kill the registration pool element before we make
+ * the index vanish forever
+ */
+ pool_put_index (am->vl_clients,
+ regp->vl_api_registration_pool_index);
+
+ vec_delete (am->vlib_private_rps, 1, i);
+ /* Kill it, accounting for the memfd header page */
+ if (munmap ((void *) virtual_base, virtual_size) < 0)
+ clib_unix_warning ("munmap");
+ /* Reset the queue-length-address cache */
+ vec_reset_length (vl_api_queue_cursizes);
+ private_registration = 1;
+ break;
+ }
+ }
+
/* No dangling references, please */
*regpp = 0;
- pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
-
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
- /* Poison the old registration */
- memset (regp, 0xF1, sizeof (*regp));
- clib_mem_free (regp);
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
+ if (private_registration == 0)
+ {
+ pool_put_index (am->vl_clients,
+ regp->vl_api_registration_pool_index);
+ pthread_mutex_lock (&svm->mutex);
+ oldheap = svm_push_data_heap (svm);
+ /* Poison the old registration */
+ memset (regp, 0xF1, sizeof (*regp));
+ clib_mem_free (regp);
+ pthread_mutex_unlock (&svm->mutex);
+ svm_pop_heap (oldheap);
+ /*
+ * These messages must be freed manually, since they're set up
+ * as "bounce" messages. In the private_registration == 1 case,
+ * we kill the shared-memory segment which contains the message
+ * with munmap.
+ */
+ vl_msg_api_free (mp);
+ }
}
else
{
vl_msg_api_send_shmem (q, (u8 *) & rmp);
}
-#define foreach_vlib_api_msg \
-_(MEMCLNT_CREATE, memclnt_create) \
-_(MEMCLNT_DELETE, memclnt_delete) \
-_(GET_FIRST_MSG_ID, get_first_msg_id)
+/**
+ * client answered a ping, stave off the grim reaper...
+ */
+
+void
+ vl_api_memclnt_keepalive_reply_t_handler
+ (vl_api_memclnt_keepalive_reply_t * mp)
+{
+ vl_api_registration_t *regp;
+ vlib_main_t *vm = vlib_get_main ();
+
+ regp = vl_api_client_index_to_registration (mp->context);
+ if (regp)
+ {
+ regp->last_heard = vlib_time_now (vm);
+ regp->unanswered_pings = 0;
+ }
+ else
+ clib_warning ("BUG: anonymous memclnt_keepalive_reply");
+}
+
+/**
+ * We can send ourselves these messages if someone uses the
+ * builtin binary api test tool...
+ */
+static void
+vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
+{
+ vl_api_memclnt_keepalive_reply_t *rmp;
+ api_main_t *am;
+ vl_shmem_hdr_t *shmem_hdr;
+
+ am = &api_main;
+ shmem_hdr = am->shmem_hdr;
+
+ rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
+ memset (rmp, 0, sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
+ rmp->context = mp->context;
+ vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
+}
+
+#define foreach_vlib_api_msg \
+_(MEMCLNT_CREATE, memclnt_create) \
+_(MEMCLNT_DELETE, memclnt_delete) \
+_(GET_FIRST_MSG_ID, get_first_msg_id) \
+_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
+_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply)
/*
* vl_api_init
memory_api_init (const char *region_name)
{
int rv;
+ api_main_t *am = &api_main;
vl_msg_api_msg_config_t cfg;
vl_msg_api_msg_config_t *c = &cfg;
foreach_vlib_api_msg;
#undef _
+ /*
+ * special-case freeing of memclnt_delete messages, so we can
+ * simply munmap pairwise / private API segments...
+ */
+ am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
+ am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
+
return 0;
}
vl_msg_api_send_shmem (q, (u8 *) & mp);
}
+static void
+send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
+{
+ vl_api_memclnt_keepalive_t *mp;
+ unix_shared_memory_queue_t *q;
+ api_main_t *am = &api_main;
+ svm_region_t *save_vlib_rp = am->vlib_rp;
+ vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
+
+ q = regp->vl_input_queue;
+
+ /*
+ * If the queue head is moving, assume that the client is processing
+ * messages and skip the ping. This heuristic may fail if the queue
+ * is in the same position as last time, net of wrapping; in which
+ * case, the client will receive a keepalive.
+ */
+ if (regp->last_queue_head != q->head)
+ {
+ regp->last_heard = now;
+ regp->unanswered_pings = 0;
+ regp->last_queue_head = q->head;
+ return;
+ }
+
+ /*
+ * push/pop shared memory segment, so this routine
+ * will work with "normal" as well as "private segment"
+ * memory clients..
+ */
+
+ am->vlib_rp = regp->vlib_rp;
+ am->shmem_hdr = regp->shmem_hdr;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+ mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
+ mp->context = mp->client_index =
+ vl_msg_api_handle_from_index_and_epoch
+ (regp->vl_api_registration_pool_index,
+ am->shmem_hdr->application_restarts);
+
+ regp->unanswered_pings++;
+
+ /* Failure-to-send due to a stuffed queue is absolutely expected */
+ if (unix_shared_memory_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
+ vl_msg_api_free (mp);
+
+ am->vlib_rp = save_vlib_rp;
+ am->shmem_hdr = save_shmem_hdr;
+}
+
+static void
+dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
+{
+
+ vl_api_registration_t **regpp;
+ vl_api_registration_t *regp;
+ static u32 *dead_indices;
+ static u32 *confused_indices;
+
+ vec_reset_length (dead_indices);
+ vec_reset_length (confused_indices);
+
+ /* *INDENT-OFF* */
+ pool_foreach (regpp, am->vl_clients,
+ ({
+ regp = *regpp;
+ if (regp)
+ {
+ /* If we haven't heard from this client recently... */
+ if (regp->last_heard < (now - 10.0))
+ {
+ if (regp->unanswered_pings == 2)
+ {
+ unix_shared_memory_queue_t *q;
+ q = regp->vl_input_queue;
+ if (kill (q->consumer_pid, 0) >=0)
+ {
+ clib_warning ("REAPER: lazy binary API client '%s'",
+ regp->name);
+ regp->unanswered_pings = 0;
+ regp->last_heard = now;
+ }
+ else
+ {
+ clib_warning ("REAPER: binary API client '%s' died",
+ regp->name);
+ vec_add1(dead_indices, regpp - am->vl_clients);
+ }
+ }
+ else
+ send_memclnt_keepalive (regp, now);
+ }
+ else
+ regp->unanswered_pings = 0;
+ }
+ else
+ {
+ clib_warning ("NULL client registration index %d",
+ regpp - am->vl_clients);
+ vec_add1 (confused_indices, regpp - am->vl_clients);
+ }
+ }));
+ /* *INDENT-ON* */
+ /* This should "never happen," but if it does, fix it... */
+ if (PREDICT_FALSE (vec_len (confused_indices) > 0))
+ {
+ int i;
+ for (i = 0; i < vec_len (confused_indices); i++)
+ {
+ pool_put_index (am->vl_clients, confused_indices[i]);
+ }
+ }
+
+ if (PREDICT_FALSE (vec_len (dead_indices) > 0))
+ {
+ int i;
+ svm_region_t *svm;
+ void *oldheap;
+
+ /* Allow the application to clean up its registrations */
+ for (i = 0; i < vec_len (dead_indices); i++)
+ {
+ regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
+ if (regpp)
+ {
+ u32 handle;
+
+ handle = vl_msg_api_handle_from_index_and_epoch
+ (dead_indices[i], shm->application_restarts);
+ (void) call_reaper_functions (handle);
+ }
+ }
+
+ svm = am->vlib_rp;
+ pthread_mutex_lock (&svm->mutex);
+ oldheap = svm_push_data_heap (svm);
+
+ for (i = 0; i < vec_len (dead_indices); i++)
+ {
+ regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
+ if (regpp)
+ {
+ /* Is this a pairwise SVM segment? */
+ if ((*regpp)->vlib_rp != svm)
+ {
+ int i;
+ svm_region_t *dead_rp = (*regpp)->vlib_rp;
+ /* Note: account for the memfd header page */
+ u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
+ u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
+
+ /* For horizontal scaling, add a hash table... */
+ for (i = 0; i < vec_len (am->vlib_private_rps); i++)
+ if (am->vlib_private_rps[i] == dead_rp)
+ {
+ vec_delete (am->vlib_private_rps, 1, i);
+ goto found;
+ }
+ clib_warning ("private rp %llx AWOL", dead_rp);
+
+ found:
+ /* Kill it, accounting for the memfd header page */
+ if (munmap ((void *) virtual_base, virtual_size) < 0)
+ clib_unix_warning ("munmap");
+ /* Reset the queue-length-address cache */
+ vec_reset_length (vl_api_queue_cursizes);
+ }
+ else
+ {
+ /* Poison the old registration */
+ memset (*regpp, 0xF3, sizeof (**regpp));
+ clib_mem_free (*regpp);
+ }
+ /* no dangling references, please */
+ *regpp = 0;
+ }
+ else
+ {
+ svm_pop_heap (oldheap);
+ clib_warning ("Duplicate free, client index %d",
+ regpp - am->vl_clients);
+ oldheap = svm_push_data_heap (svm);
+ }
+ }
+
+ svm_client_scan_this_region_nolock (am->vlib_rp);
+
+ pthread_mutex_unlock (&svm->mutex);
+ svm_pop_heap (oldheap);
+ for (i = 0; i < vec_len (dead_indices); i++)
+ pool_put_index (am->vl_clients, dead_indices[i]);
+ }
+}
+
+
static uword
memclnt_process (vlib_main_t * vm,
vlib_node_runtime_t * node, vlib_frame_t * f)
f64 dead_client_scan_time;
f64 sleep_time, start_time;
f64 vector_rate;
+ clib_error_t *socksvr_api_init (vlib_main_t * vm);
+ clib_error_t *error;
int i;
- u8 *serialized_message_table = 0;
- svm_region_t *svm;
- void *oldheap;
+ vl_socket_args_for_process_t *a;
+ uword event_type;
+ uword *event_data = 0;
+ int private_segment_rotor = 0;
+ svm_region_t *vlib_rp;
+ f64 now;
vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
if ((rv = memory_api_init (am->region_name)) < 0)
{
- clib_warning ("memory_api_init returned %d, wait for godot...", rv);
- vlib_process_suspend (vm, 1e70);
+ clib_warning ("memory_api_init returned %d, quitting...", rv);
+ return 0;
+ }
+
+ if ((error = socksvr_api_init (vm)))
+ {
+ clib_error_report (error);
+ clib_warning ("socksvr_api_init failed, quitting...");
+ return 0;
}
shm = am->shmem_hdr;
if (e)
clib_error_report (e);
- sleep_time = 20.0;
- dead_client_scan_time = vlib_time_now (vm) + 20.0;
+ sleep_time = 10.0;
+ dead_client_scan_time = vlib_time_now (vm) + 10.0;
/*
* Send plugin message range messages for each plugin we loaded
rp->last_msg_id);
}
- /*
- * Snapshoot the api message table.
- */
- serialized_message_table = vl_api_serialize_message_table (am, 0);
-
- svm = am->vlib_rp;
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
-
- am->serialized_message_table_in_shmem = vec_dup (serialized_message_table);
-
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
-
/*
* Save the api message table snapshot, if configured
*/
{
int fd, rv;
u8 *chroot_file;
+ u8 *serialized_message_table;
+
+ /*
+ * Snapshoot the api message table.
+ */
if (strstr ((char *) am->save_msg_table_filename, "..")
|| index ((char *) am->save_msg_table_filename, '/'))
{
clib_unix_warning ("creat");
goto skip_save;
}
+
+ serialized_message_table = vl_api_serialize_message_table (am, 0);
+
rv = write (fd, serialized_message_table,
vec_len (serialized_message_table));
clib_unix_warning ("close");
vec_free (chroot_file);
+ vec_free (serialized_message_table);
}
skip_save:
- vec_free (serialized_message_table);
/* $$$ pay attention to frame size, control CPU usage */
while (1)
{
- uword event_type __attribute__ ((unused));
i8 *headp;
int need_broadcast;
}
}
- event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time);
- vm->queue_signal_pending = 0;
- vlib_process_get_events (vm, 0 /* event_data */ );
-
- if (vlib_time_now (vm) > dead_client_scan_time)
+ /*
+ * see if we have any private api shared-memory segments
+ * If so, push required context variables, and process
+ * a message.
+ */
+ if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
{
- vl_api_registration_t **regpp;
- vl_api_registration_t *regp;
- unix_shared_memory_queue_t *q;
- static u32 *dead_indices;
- static u32 *confused_indices;
+ unix_shared_memory_queue_t *save_vlib_input_queue = q;
+ vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
+ svm_region_t *save_vlib_rp = am->vlib_rp;
- vec_reset_length (dead_indices);
- vec_reset_length (confused_indices);
+ vlib_rp = am->vlib_rp = am->vlib_private_rps[private_segment_rotor];
- /* *INDENT-OFF* */
- pool_foreach (regpp, am->vl_clients,
- ({
- regp = *regpp;
- if (regp)
- {
- q = regp->vl_input_queue;
- if (kill (q->consumer_pid, 0) < 0)
- {
- vec_add1(dead_indices, regpp - am->vl_clients);
- }
- }
- else
- {
- clib_warning ("NULL client registration index %d",
- regpp - am->vl_clients);
- vec_add1 (confused_indices, regpp - am->vl_clients);
- }
- }));
- /* *INDENT-ON* */
- /* This should "never happen," but if it does, fix it... */
- if (PREDICT_FALSE (vec_len (confused_indices) > 0))
- {
- int i;
- for (i = 0; i < vec_len (confused_indices); i++)
- {
- pool_put_index (am->vl_clients, confused_indices[i]);
- }
- }
+ am->shmem_hdr = (void *) vlib_rp->user_ctx;
+ q = am->shmem_hdr->vl_input_queue;
- if (PREDICT_FALSE (vec_len (dead_indices) > 0))
+ pthread_mutex_lock (&q->mutex);
+ if (q->cursize > 0)
{
- int i;
- svm_region_t *svm;
- void *oldheap;
+ headp = (i8 *) (q->data + sizeof (uword) * q->head);
+ clib_memcpy (&mp, headp, sizeof (uword));
- /* Allow the application to clean up its registrations */
- for (i = 0; i < vec_len (dead_indices); i++)
- {
- regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
- if (regpp)
- {
- u32 handle;
-
- handle = vl_msg_api_handle_from_index_and_epoch
- (dead_indices[i], shm->application_restarts);
- (void) call_reaper_functions (handle);
- }
- }
+ q->head++;
+ need_broadcast = (q->cursize == q->maxsize / 2);
+ q->cursize--;
- svm = am->vlib_rp;
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
+ if (PREDICT_FALSE (q->head == q->maxsize))
+ q->head = 0;
+ pthread_mutex_unlock (&q->mutex);
+ if (need_broadcast)
+ (void) pthread_cond_broadcast (&q->condvar);
- for (i = 0; i < vec_len (dead_indices); i++)
- {
- regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
- if (regpp)
- {
- /* Poison the old registration */
- memset (*regpp, 0xF3, sizeof (**regpp));
- clib_mem_free (*regpp);
- /* no dangling references, please */
- *regpp = 0;
- }
- else
- {
- svm_pop_heap (oldheap);
- clib_warning ("Duplicate free, client index %d",
- regpp - am->vl_clients);
- oldheap = svm_push_data_heap (svm);
- }
- }
+ pthread_mutex_unlock (&q->mutex);
- svm_client_scan_this_region_nolock (am->vlib_rp);
+ vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
+ }
+ else
+ pthread_mutex_unlock (&q->mutex);
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
- for (i = 0; i < vec_len (dead_indices); i++)
- pool_put_index (am->vl_clients, dead_indices[i]);
+ q = save_vlib_input_queue;
+ am->shmem_hdr = save_shmem_hdr;
+ am->vlib_rp = save_vlib_rp;
+
+ private_segment_rotor++;
+ if (private_segment_rotor >= vec_len (am->vlib_private_rps))
+ private_segment_rotor = 0;
+ }
+
+ vlib_process_wait_for_event_or_clock (vm, sleep_time);
+ vec_reset_length (event_data);
+ event_type = vlib_process_get_events (vm, &event_data);
+ now = vlib_time_now (vm);
+
+ switch (event_type)
+ {
+ case QUEUE_SIGNAL_EVENT:
+ vm->queue_signal_pending = 0;
+ break;
+
+ case SOCKET_READ_EVENT:
+ for (i = 0; i < vec_len (event_data); i++)
+ {
+ a = pool_elt_at_index (socket_main.process_args, event_data[i]);
+ vl_api_socket_process_msg (a->clib_file, a->regp,
+ (i8 *) a->data);
+ vec_free (a->data);
+ pool_put (socket_main.process_args, a);
}
+ break;
- dead_client_scan_time = vlib_time_now (vm) + 20.0;
+ /* Timeout... */
+ case -1:
+ break;
+
+ default:
+ clib_warning ("unknown event type %d", event_type);
+ break;
+ }
+
+ if (now > dead_client_scan_time)
+ {
+ dead_client_scan (am, shm, now);
+ dead_client_scan_time = vlib_time_now (vm) + 10.0;
}
if (TRACE_VLIB_MEMORY_QUEUE)
return 0;
}
/* *INDENT-OFF* */
-VLIB_REGISTER_NODE (memclnt_node,static) = {
- .function = memclnt_process,
- .type = VLIB_NODE_TYPE_PROCESS,
- .name = "api-rx-from-ring",
- .state = VLIB_NODE_STATE_DISABLED,
+VLIB_REGISTER_NODE (memclnt_node) =
+{
+ .function = memclnt_process,
+ .type = VLIB_NODE_TYPE_PROCESS,
+ .name = "api-rx-from-ring",
+ .state = VLIB_NODE_STATE_DISABLED,
};
/* *INDENT-ON* */
};
/* *INDENT-ON* */
+volatile int **vl_api_queue_cursizes;
+
static void
memclnt_queue_callback (vlib_main_t * vm)
{
- static volatile int *cursizep;
+ int i;
+ api_main_t *am = &api_main;
- if (PREDICT_FALSE (cursizep == 0))
+ if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
+ 1 + vec_len (am->vlib_private_rps)))
{
- api_main_t *am = &api_main;
vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
unix_shared_memory_queue_t *q;
q = shmem_hdr->vl_input_queue;
if (q == 0)
return;
- cursizep = &q->cursize;
+
+ vec_add1 (vl_api_queue_cursizes, &q->cursize);
+
+ for (i = 0; i < vec_len (am->vlib_private_rps); i++)
+ {
+ svm_region_t *vlib_rp = am->vlib_private_rps[i];
+
+ shmem_hdr = (void *) vlib_rp->user_ctx;
+ q = shmem_hdr->vl_input_queue;
+ vec_add1 (vl_api_queue_cursizes, &q->cursize);
+ }
}
- if (*cursizep >= 1)
+ for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
{
- vm->queue_signal_pending = 1;
- vm->api_queue_nonempty = 1;
- vlib_process_signal_event (vm, memclnt_node.index,
- /* event_type */ 0, /* event_data */ 0);
+ if (*vl_api_queue_cursizes[i])
+ {
+ vm->queue_signal_pending = 1;
+ vm->api_queue_nonempty = 1;
+ vlib_process_signal_event (vm, memclnt_node.index,
+ /* event_type */ QUEUE_SIGNAL_EVENT,
+ /* event_data */ 0);
+ break;
+ }
}
}
VLIB_INIT_FUNCTION (setup_memclnt_exit);
+u8 *
+format_api_message_rings (u8 * s, va_list * args)
+{
+ api_main_t *am = va_arg (*args, api_main_t *);
+ vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
+ int main_segment = va_arg (*args, int);
+ ring_alloc_t *ap;
+ int i;
+
+ if (shmem_hdr == 0)
+ return format (s, "%8s %8s %8s %8s %8s\n",
+ "Owner", "Size", "Nitems", "Hits", "Misses");
+
+ ap = shmem_hdr->vl_rings;
+
+ for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
+ {
+ s = format (s, "%8s %8d %8d %8d %8d\n",
+ "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
+ ap++;
+ }
+
+ ap = shmem_hdr->client_rings;
+
+ for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
+ {
+ s = format (s, "%8s %8d %8d %8d %8d\n",
+ "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
+ ap++;
+ }
+
+ if (main_segment)
+ {
+ s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
+ s = format
+ (s,
+ "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
+ shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
+ shmem_hdr->garbage_collects);
+ }
+ return s;
+}
+
static clib_error_t *
vl_api_ring_command (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cli_cmd)
{
int i;
- ring_alloc_t *ap;
vl_shmem_hdr_t *shmem_hdr;
api_main_t *am = &api_main;
return 0;
}
- vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
- "Owner", "Size", "Nitems", "Hits", "Misses");
-
- ap = shmem_hdr->vl_rings;
+ vlib_cli_output (vm, "Main API segment rings:");
- for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
- {
- vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
- "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
- ap++;
- }
+ vlib_cli_output (vm, "%U", format_api_message_rings, am,
+ 0 /* print header */ , 0 /* notused */ );
- ap = shmem_hdr->client_rings;
+ vlib_cli_output (vm, "%U", format_api_message_rings, am,
+ shmem_hdr, 1 /* main segment */ );
- for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
+ for (i = 0; i < vec_len (am->vlib_private_rps); i++)
{
- vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
- "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
- ap++;
+ svm_region_t *vlib_rp = am->vlib_private_rps[i];
+ shmem_hdr = (void *) vlib_rp->user_ctx;
+ vl_api_registration_t **regpp;
+ vl_api_registration_t *regp;
+
+ /* For horizontal scaling, add a hash table... */
+ /* *INDENT-OFF* */
+ pool_foreach (regpp, am->vl_clients,
+ ({
+ regp = *regpp;
+ if (regp && regp->vlib_rp == vlib_rp)
+ {
+ vlib_cli_output (vm, "%s segment rings:", regp->name);
+ goto found;
+ }
+ }));
+ /* *INDENT-ON* */
+ found:
+ vlib_cli_output (vm, "%U", format_api_message_rings, am,
+ shmem_hdr, 0 /* main segment */ );
}
- vlib_cli_output (vm, "%d ring miss fallback allocations\n",
- am->ring_misses);
-
- vlib_cli_output
- (vm, "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
- shmem_hdr->application_restarts,
- shmem_hdr->restart_reclaims, shmem_hdr->garbage_collects);
return 0;
}
if (regp)
{
- q = regp->vl_input_queue;
- if (kill (q->consumer_pid, 0) < 0)
- {
- health = "DEAD";
- }
+ if (regp->unanswered_pings > 0)
+ health = "questionable";
else
- {
- health = "alive";
- }
+ health = "OK";
+
+ q = regp->vl_input_queue;
+
vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
regp->name, q->consumer_pid, q->cursize,
q, health);
{
api_main_t *am = &api_main;
svm_map_region_args_t _a, *a = &_a;
+ clib_error_t *error;
memset (a, 0, sizeof (*a));
a->root_path = am->root_path;
0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
svm_region_init_args (a);
- return 0;
+
+ error = vlib_call_init_function (vm, vlibsocket_init);
+
+ return error;
}
VLIB_INIT_FUNCTION (vlibmemory_init);
/* Load the serialized message table from the table dump */
- error = unserialize_open_unix_file (sm, (char *) filename);
+ error = unserialize_open_clib_file (sm, (char *) filename);
if (error)
return error;
if (compare_current)
{
/* Append the current message table */
- u8 *tblv = vec_dup (am->serialized_message_table_in_shmem);
+ u8 *tblv = vl_api_serialize_message_table (am, 0);
serialize_open_vector (sm, tblv);
unserialize_integer (sm, &nmsgs, sizeof (u32));
item->crc = extract_crc (name_and_crc);
item->which = 1; /* current_image */
}
+ vec_free (tblv);
}
/* Sort the table. */
--- /dev/null
+/*
+ *------------------------------------------------------------------
+ * socket_client.c - API message handling over sockets, client code.
+ *
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <setjmp.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <time.h>
+#include <fcntl.h>
+#include <string.h>
+#include <vppinfra/clib.h>
+#include <vppinfra/vec.h>
+#include <vppinfra/hash.h>
+#include <vppinfra/bitmap.h>
+#include <vppinfra/fifo.h>
+#include <vppinfra/time.h>
+#include <vppinfra/mheap.h>
+#include <vppinfra/heap.h>
+#include <vppinfra/pool.h>
+#include <vppinfra/format.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include <vlibmemory/api.h>
+
+#include <vlibmemory/vl_memory_msg_enum.h>
+
+#define vl_typedefs /* define message structures */
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_typedefs
+
+#define vl_endianfun /* define message structures */
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
+#define vl_printfun
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_printfun
+
+socket_client_main_t socket_client_main;
+
+/* Debug aid */
+u32 vl (void *p) __attribute__ ((weak));
+u32
+vl (void *p)
+{
+ return vec_len (p);
+}
+
+void
+vl_socket_client_read_reply (socket_client_main_t * scm)
+{
+ int n, current_rx_index;
+ msgbuf_t *mbp;
+
+ if (scm->socket_fd == 0 || scm->socket_enable == 0)
+ return;
+
+ mbp = 0;
+
+ while (1)
+ {
+ current_rx_index = vec_len (scm->socket_rx_buffer);
+ while (vec_len (scm->socket_rx_buffer) <
+ sizeof (*mbp) + 2 /* msg id */ )
+ {
+ vec_validate (scm->socket_rx_buffer, current_rx_index
+ + scm->socket_buffer_size - 1);
+ _vec_len (scm->socket_rx_buffer) = current_rx_index;
+ n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
+ scm->socket_buffer_size);
+ if (n < 0)
+ {
+ clib_unix_warning ("socket_read");
+ return;
+ }
+ _vec_len (scm->socket_rx_buffer) += n;
+ }
+
+#if CLIB_DEBUG > 1
+ if (n > 0)
+ clib_warning ("read %d bytes", n);
+#endif
+
+ if (mbp == 0)
+ mbp = (msgbuf_t *) (scm->socket_rx_buffer);
+
+ if (vec_len (scm->socket_rx_buffer) >= ntohl (mbp->data_len)
+ + sizeof (*mbp))
+ {
+ vl_msg_api_socket_handler ((void *) (mbp->data));
+
+ if (vec_len (scm->socket_rx_buffer) == ntohl (mbp->data_len)
+ + sizeof (*mbp))
+ _vec_len (scm->socket_rx_buffer) = 0;
+ else
+ vec_delete (scm->socket_rx_buffer, ntohl (mbp->data_len)
+ + sizeof (*mbp), 0);
+ mbp = 0;
+
+ /* Quit if we're out of data, and not expecting a ping reply */
+ if (vec_len (scm->socket_rx_buffer) == 0
+ && scm->control_pings_outstanding == 0)
+ break;
+ }
+ }
+}
+
+int
+vl_socket_client_connect (socket_client_main_t * scm, char *socket_path,
+ char *client_name, u32 socket_buffer_size)
+{
+ char buffer[256];
+ char *rdptr;
+ int n, total_bytes;
+ vl_api_sockclnt_create_reply_t *rp;
+ vl_api_sockclnt_create_t *mp;
+ clib_socket_t *sock = &scm->client_socket;
+ msgbuf_t *mbp;
+ clib_error_t *error;
+
+ /* Already connected? */
+ if (scm->socket_fd)
+ return (-2);
+
+ /* bogus call? */
+ if (socket_path == 0 || client_name == 0)
+ return (-3);
+
+ sock->config = socket_path;
+ sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
+
+ error = clib_socket_init (sock);
+
+ if (error)
+ {
+ clib_error_report (error);
+ return (-1);
+ }
+
+ scm->socket_fd = sock->fd;
+
+ mbp = (msgbuf_t *) buffer;
+ mbp->q = 0;
+ mbp->data_len = ntohl (sizeof (*mp));
+ mbp->gc_mark_timestamp = 0;
+
+ mp = (vl_api_sockclnt_create_t *) mbp->data;
+ mp->_vl_msg_id = ntohs (VL_API_SOCKCLNT_CREATE);
+ strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
+ mp->name[sizeof (mp->name) - 1] = 0;
+ mp->context = 0xfeedface;
+
+ n = write (scm->socket_fd, mbp, sizeof (*mbp) + ntohl (mbp->data_len));
+ if (n < 0)
+ {
+ clib_unix_warning ("socket write (msg)");
+ return (-1);
+ }
+
+ memset (buffer, 0, sizeof (buffer));
+
+ total_bytes = 0;
+ rdptr = buffer;
+ do
+ {
+ n = read (scm->socket_fd, rdptr, sizeof (buffer) - (rdptr - buffer));
+ if (n < 0)
+ {
+ clib_unix_warning ("socket read");
+ }
+ total_bytes += n;
+ rdptr += n;
+ }
+ while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t)
+ + sizeof (msgbuf_t));
+
+ rp = (vl_api_sockclnt_create_reply_t *) (buffer + sizeof (msgbuf_t));
+ if (ntohs (rp->_vl_msg_id) != VL_API_SOCKCLNT_CREATE_REPLY)
+ {
+ clib_warning ("connect reply got msg id %d\n", ntohs (rp->_vl_msg_id));
+ return (-1);
+ }
+
+ /* allocate tx, rx buffers */
+ scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
+ SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
+ vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
+ vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
+ _vec_len (scm->socket_rx_buffer) = 0;
+ scm->socket_enable = 1;
+
+ return (0);
+}
+
+void
+vl_socket_client_disconnect (socket_client_main_t * scm)
+{
+ if (scm->socket_fd && (close (scm->socket_fd) < 0))
+ clib_unix_warning ("close");
+ scm->socket_fd = 0;
+}
+
+void
+vl_socket_client_enable_disable (socket_client_main_t * scm, int enable)
+{
+ scm->socket_enable = enable;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <vppinfra/byte_order.h>
+#include <svm/memfd.h>
#include <fcntl.h>
#include <sys/stat.h>
-#include <vlibsocket/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/vl_socket_msg_enum.h> /* enumerate all vlib messages */
+#include <vlibmemory/vl_memory_msg_enum.h>
#define vl_typedefs /* define message structures */
-#include <vlibsocket/vl_socket_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
#undef vl_typedefs
/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
-#include <vlibsocket/vl_socket_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
#undef vl_printfun
/* instantiate all the endian swap functions we know about */
#define vl_endianfun
-#include <vlibsocket/vl_socket_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
#undef vl_endianfun
-socket_main_t socket_main;
-
void
dump_socket_clients (vlib_main_t * vm, api_main_t * am)
{
if (pool_elts (sm->registration_pool) < 2)
return;
- vlib_cli_output (vm, "TCP socket clients");
+ vlib_cli_output (vm, "Socket clients");
vlib_cli_output (vm, "%16s %8s", "Name", "Fildesc");
/* *INDENT-OFF* */
pool_foreach (reg, sm->registration_pool,
void
vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
{
- u32 nbytes = 4; /* for the length... */
u16 msg_id = ntohs (*(u16 *) elem);
- u32 msg_length;
- u32 tmp;
api_main_t *am = &api_main;
+ msgbuf_t *mb = (msgbuf_t *) (elem - offsetof (msgbuf_t, data));
+#if CLIB_DEBUG > 1
+ u32 output_length;
+#endif
+ clib_file_t *cf = rp->clib_file_index + file_main.file_pool;
ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
return;
}
- msg_length = am->api_trace_cfg[msg_id].size;
- nbytes += msg_length;
- tmp = clib_host_to_net_u32 (nbytes);
-
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
- rp->vl_api_registration_pool_index
- + socket_main.registration_pool,
- (u8 *) & tmp, sizeof (tmp));
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
+ /* Add the msgbuf_t to the output vector */
+ vl_socket_add_pending_output_no_flush (cf,
+ rp->vl_api_registration_pool_index +
+ socket_main.registration_pool,
+ (u8 *) mb, sizeof (*mb));
+ /* Send the message */
+ vl_socket_add_pending_output (cf,
rp->vl_api_registration_pool_index
+ socket_main.registration_pool,
- elem, msg_length);
- vl_msg_api_free ((void *) elem);
-}
-
-void
-vl_socket_api_send_with_data (vl_api_registration_t * rp,
- u8 * elem, u8 * data_vector)
-{
- u32 nbytes = 4; /* for the length... */
- u16 msg_id = ntohs (*(u16 *) elem);
- u32 msg_length;
- u32 tmp;
- api_main_t *am = &api_main;
-
- ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
-
- if (msg_id >= vec_len (am->api_trace_cfg))
- {
- clib_warning ("id out of range: %d", msg_id);
- vec_free (data_vector);
- vl_msg_api_free ((void *) elem);
- return;
- }
-
- msg_length = am->api_trace_cfg[msg_id].size;
- nbytes += msg_length;
- nbytes += vec_len (data_vector);
+ elem, ntohl (mb->data_len));
- /* Length in network byte order */
- tmp = clib_host_to_net_u32 (nbytes);
+#if CLIB_DEBUG > 1
+ output_length = sizeof (*mb) + ntohl (mb->data_len);
+ clib_warning ("wrote %u bytes to fd %d", output_length,
+ cf->file_descriptor);
+#endif
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
- rp->vl_api_registration_pool_index
- + socket_main.registration_pool,
- (u8 *) & tmp, sizeof (tmp));
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
- rp->vl_api_registration_pool_index
- + socket_main.registration_pool,
- elem, msg_length);
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
- rp->vl_api_registration_pool_index
- + socket_main.registration_pool,
- data_vector, vec_len (data_vector));
vl_msg_api_free ((void *) elem);
}
-static inline void
-vl_socket_api_send_with_length_internal (vl_api_registration_t * rp,
- u8 * elem, u32 msg_length, int free)
-{
- u32 nbytes = 4; /* for the length... */
- u16 msg_id = ntohs (*(u16 *) elem);
- u32 tmp;
- api_main_t *am = &api_main;
-
- ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
-
- if (msg_id >= vec_len (am->api_trace_cfg))
- {
- clib_warning ("id out of range: %d", msg_id);
- if (free)
- vl_msg_api_free ((void *) elem);
- return;
- }
-
- nbytes += msg_length;
-
- /* Length in network byte order */
- tmp = clib_host_to_net_u32 (nbytes);
-
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
- rp->vl_api_registration_pool_index
- + socket_main.registration_pool,
- (u8 *) & tmp, sizeof (tmp));
- vl_socket_add_pending_output (rp->clib_file_index
- + file_main.file_pool,
- rp->vl_api_registration_pool_index
- + socket_main.registration_pool,
- elem, msg_length);
- if (free)
- vl_msg_api_free ((void *) elem);
-}
-
-void
-vl_socket_api_send_with_length (vl_api_registration_t * rp,
- u8 * elem, u32 msg_length)
-{
- vl_socket_api_send_with_length_internal (rp, elem, msg_length,
- 1 /* free */ );
-}
-
-void
-vl_socket_api_send_with_length_no_free (vl_api_registration_t * rp,
- u8 * elem, u32 msg_length)
-{
- vl_socket_api_send_with_length_internal (rp, elem, msg_length,
- 0 /* free */ );
-}
-
void
vl_free_socket_registration_index (u32 pool_index)
{
+ int i;
vl_api_registration_t *rp;
if (pool_is_free_index (socket_main.registration_pool, pool_index))
{
rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE);
+ for (i = 0; i < vec_len (rp->additional_fds_to_close); i++)
+ if (close (rp->additional_fds_to_close[i]) < 0)
+ clib_unix_warning ("close");
+ vec_free (rp->additional_fds_to_close);
vec_free (rp->name);
vec_free (rp->unprocessed_input);
vec_free (rp->output_vector);
pool_put (socket_main.registration_pool, rp);
}
-static inline void
-socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
- i8 * input_v)
+void
+vl_api_socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
+ i8 * input_v)
{
- u8 *the_msg = (u8 *) (input_v + sizeof (u32));
+ msgbuf_t *mbp = (msgbuf_t *) input_v;
+
+ u8 *the_msg = (u8 *) (mbp->data);
socket_main.current_uf = uf;
socket_main.current_rp = rp;
vl_msg_api_socket_handler (the_msg);
vl_socket_read_ready (clib_file_t * uf)
{
clib_file_main_t *fm = &file_main;
+ vlib_main_t *vm = vlib_get_main ();
vl_api_registration_t *rp;
int n;
i8 *msg_buffer = 0;
+ u8 *data_for_process;
u32 msg_len;
u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
+ vl_socket_args_for_process_t *a;
+ msgbuf_t *mbp;
+ int mbp_set = 0;
rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
/*
* Look for bugs here. This code is tricky because
- * data read from a stream socket does honor message
+ * data read from a stream socket does not honor message
* boundaries. In the case of a long message (>4K bytes)
* we have to do (at least) 2 reads, etc.
*/
{
vec_append (rp->unprocessed_input, socket_main.input_buffer);
msg_buffer = rp->unprocessed_input;
- msg_len = rp->unprocessed_msg_length;
}
else
{
msg_buffer = socket_main.input_buffer;
- msg_len = 0;
+ mbp_set = 0;
}
- if (msg_len == 0)
+ if (mbp_set == 0)
{
- /* Length may be split across two reads */
- if (vec_len (msg_buffer) < sizeof (u32))
+ /* Any chance that we have a complete message? */
+ if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
goto save_and_split;
- /* total length, including msg_len itself, in network byte order */
- msg_len = clib_net_to_host_u32 (*((u32 *) msg_buffer));
- }
-
- /* Happens if the client sent msg_len == 0 */
- if (msg_len == 0)
- {
- clib_warning ("msg_len == 0");
- goto turf_it;
+ mbp = (msgbuf_t *) msg_buffer;
+ msg_len = ntohl (mbp->data_len);
+ mbp_set = 1;
}
/* We don't have the entire message yet. */
- if (msg_len > vec_len (msg_buffer))
+ if (mbp_set == 0
+ || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
{
save_and_split:
- /*
- * if we were using the shared input buffer,
- * save the fragment.
- */
+ /* if we were using the input buffer save the fragment */
if (msg_buffer == socket_main.input_buffer)
{
ASSERT (vec_len (rp->unprocessed_input) == 0);
_vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
}
_vec_len (socket_main.input_buffer) = save_input_buffer_length;
- rp->unprocessed_msg_length = msg_len;
return 0;
}
- socket_process_msg (uf, rp, msg_buffer);
- if (n > msg_len)
- vec_delete (msg_buffer, msg_len, 0);
+ data_for_process = (u8 *) vec_dup (msg_buffer);
+ _vec_len (data_for_process) = (msg_len + sizeof (msgbuf_t));
+ pool_get (socket_main.process_args, a);
+ a->clib_file = uf;
+ a->regp = rp;
+ a->data = data_for_process;
+
+ vlib_process_signal_event (vm, memclnt_node.index,
+ SOCKET_READ_EVENT,
+ a - socket_main.process_args);
+ if (n > (msg_len + sizeof (*mbp)))
+ vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
else
_vec_len (msg_buffer) = 0;
- n -= msg_len;
+ n -= msg_len + sizeof (msgbuf_t);
msg_len = 0;
- rp->unprocessed_msg_length = 0;
+ mbp_set = 0;
}
while (n > 0);
-turf_it:
_vec_len (socket_main.input_buffer) = save_input_buffer_length;
return 0;
}
}
+void
+vl_socket_add_pending_output_no_flush (clib_file_t * uf,
+ vl_api_registration_t * rp,
+ u8 * buffer, uword buffer_bytes)
+{
+ vec_add (rp->output_vector, buffer, buffer_bytes);
+}
+
static void
socket_del_pending_output (clib_file_t * uf,
vl_api_registration_t * rp, uword n_bytes)
socksvr_accept_ready (clib_file_t * uf)
{
clib_file_main_t *fm = &file_main;
- struct sockaddr_in client_addr;
- int client_fd;
- int client_len;
+ socket_main_t *sm = &socket_main;
+ clib_socket_t *sock = &sm->socksvr_listen_socket;
+ clib_socket_t client;
+ clib_error_t *error;
- client_len = sizeof (client_addr);
+ error = clib_socket_accept (sock, &client);
- /*
- * Supposedly acquires the non-blocking attrib from the
- * server socket.
- */
- client_fd = accept (uf->file_descriptor,
- (struct sockaddr *) &client_addr,
- (socklen_t *) & client_len);
+ if (error)
+ return error;
- if (client_fd < 0)
- return clib_error_return_unix (0, "socksvr_accept_ready: accept");
-
- socksvr_file_add (fm, client_fd);
+ socksvr_file_add (fm, client.fd);
return 0;
}
}
}
-#define foreach_vlib_api_msg \
-_(SOCKCLNT_CREATE, sockclnt_create) \
-_(SOCKCLNT_DELETE, sockclnt_delete)
-
static clib_error_t *
-socksvr_api_init (vlib_main_t * vm)
+send_fd_msg (int socket_fd, int fd_to_share)
{
- clib_file_main_t *fm = &file_main;
- clib_file_t template = { 0 };
- int sockfd;
- int one = 1;
+ struct msghdr mh = { 0 };
+ struct iovec iov[1];
+ char ctl[CMSG_SPACE (sizeof (int))];
+ char *msg = "memfd";
int rv;
- struct sockaddr_in serv_addr;
- vl_api_registration_t *rp;
- u16 portno;
- u32 bind_address;
-
-#define _(N,n) \
- vl_msg_api_set_handlers(VL_API_##N, #n, \
- vl_api_##n##_t_handler, \
- vl_noop_handler, \
- vl_api_##n##_t_endian, \
- vl_api_##n##_t_print, \
- sizeof(vl_api_##n##_t), 1);
- foreach_vlib_api_msg;
-#undef _
- vec_resize (socket_main.input_buffer, 4096);
+ iov[0].iov_base = msg;
+ iov[0].iov_len = strlen (msg);
+ mh.msg_iov = iov;
+ mh.msg_iovlen = 1;
+
+ struct cmsghdr *cmsg;
+ memset (&ctl, 0, sizeof (ctl));
+ mh.msg_control = ctl;
+ mh.msg_controllen = sizeof (ctl);
+ cmsg = CMSG_FIRSTHDR (&mh);
+ cmsg->cmsg_len = CMSG_LEN (sizeof (int));
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ memcpy (CMSG_DATA (cmsg), &fd_to_share, sizeof (int));
+
+ rv = sendmsg (socket_fd, &mh, 0);
+ if (rv < 0)
+ return clib_error_return_unix (0, "sendmsg");
+ return 0;
+}
- /* Set up non-blocking server socket on CLIENT_API_SERVER_PORT */
- sockfd = socket (AF_INET, SOCK_STREAM, 0);
+/*
+ * Create a memory-fd segment.
+ */
+void
+vl_api_memfd_segment_create_t_handler (vl_api_memfd_segment_create_t * mp)
+{
+ vl_api_memfd_segment_create_reply_t *rmp;
+ api_main_t *am = &api_main;
+ clib_file_t *cf;
+ memfd_private_t _memfd_private, *memfd = &_memfd_private;
+ vl_api_registration_t *regp;
+ vlib_main_t *vm = vlib_get_main ();
+ svm_map_region_args_t _args, *a = &_args;
+ svm_region_t *vlib_rp;
+ int rv;
- if (sockfd < 0)
- {
- return clib_error_return_unix (0, "socket");
- }
+ regp = vl_api_client_index_to_registration (mp->client_index);
- rv = ioctl (sockfd, FIONBIO, &one);
- if (rv < 0)
+ if (regp == 0)
{
- close (sockfd);
- return clib_error_return_unix (0, "FIONBIO");
+ clib_warning ("API client disconnected");
+ return;
}
- rv = setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one));
- if (rv < 0)
+ if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
{
- close (sockfd);
- return clib_error_return_unix (0, "SO_REUSEADDR");
+ rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */
+ goto reply;
}
- bzero ((char *) &serv_addr, sizeof (serv_addr));
- serv_addr.sin_family = AF_INET;
+ memset (memfd, 0, sizeof (*memfd));
- if (socket_main.bind_address)
- bind_address = socket_main.bind_address;
- else
- bind_address = INADDR_LOOPBACK;
+ /* Embed in api_main_t */
+ memfd->memfd_size = mp->requested_size;
+ memfd->requested_va = 0ULL;
+ memfd->i_am_master = 1;
+ memfd->name = format (0, "%s%c", regp->name, 0);
- if (socket_main.portno)
- portno = socket_main.portno;
- else
- portno = SOCKSVR_DEFAULT_PORT;
+ /* Set up a memfd segment of the requested size */
+ rv = memfd_master_init (memfd, mp->client_index);
- serv_addr.sin_port = clib_host_to_net_u16 (portno);
- serv_addr.sin_addr.s_addr = clib_host_to_net_u32 (bind_address);
+ if (rv)
+ goto reply;
- if (bind (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0)
- {
- close (sockfd);
- return clib_error_return_unix (0, "bind");
- }
+ /* Remember to close this fd when the socket connection goes away */
+ vec_add1 (regp->additional_fds_to_close, memfd->fd);
- rv = listen (sockfd, 5);
- if (rv < 0)
+ /* And create a plausible svm_region in it */
+ memset (a, 0, sizeof (*a));
+ a->baseva = memfd->sh->memfd_va + MMAP_PAGESIZE;
+ a->size = memfd->memfd_size - MMAP_PAGESIZE;
+ /* $$$$ might want a different config parameter */
+ a->pvt_heap_size = am->api_pvt_heap_size;
+ a->flags = SVM_FLAGS_MHEAP;
+ svm_region_init_mapped_region (a, (svm_region_t *) a->baseva);
+
+ vlib_rp = (svm_region_t *) a->baseva;
+
+ /*
+ * Part deux, initialize the svm_region_t shared-memory header
+ * api allocation rings, and so on.
+ */
+ vl_init_shmem (vlib_rp, 1 /* is_vlib (dont-care) */ , 1 /* is_private */ );
+
+ vec_add1 (am->vlib_private_rps, vlib_rp);
+
+ memfd->sh->ready = 1;
+
+ /* Recompute the set of input queues to poll in memclnt_process */
+ vec_reset_length (vl_api_queue_cursizes);
+
+reply:
+
+ /* send the reply message */
+
+ rmp = vl_msg_api_alloc (sizeof (*rmp));
+ rmp->_vl_msg_id = htons (VL_API_MEMFD_SEGMENT_CREATE_REPLY);
+ rmp->context = mp->context;
+ rmp->retval = htonl (rv);
+
+ vl_msg_api_send (regp, (u8 *) rmp);
+
+ if (rv != 0)
+ return;
+
+ /*
+ * We need the reply message to make it out the back door
+ * before we send the magic fd message.
+ */
+ vlib_process_suspend (vm, 11e-6);
+
+ cf = file_main.file_pool + regp->clib_file_index;
+
+ /* send the magic "here's your sign (aka fd)" socket message */
+ send_fd_msg (cf->file_descriptor, memfd->fd);
+}
+
+#define foreach_vlib_api_msg \
+_(SOCKCLNT_CREATE, sockclnt_create) \
+_(SOCKCLNT_DELETE, sockclnt_delete) \
+_(MEMFD_SEGMENT_CREATE, memfd_segment_create)
+
+clib_error_t *
+socksvr_api_init (vlib_main_t * vm)
+{
+ clib_file_main_t *fm = &file_main;
+ clib_file_t template = { 0 };
+ vl_api_registration_t *rp;
+ vl_msg_api_msg_config_t cfg;
+ vl_msg_api_msg_config_t *c = &cfg;
+ socket_main_t *sm = &socket_main;
+ clib_socket_t *sock = &sm->socksvr_listen_socket;
+ clib_error_t *error;
+
+ /* If not explicitly configured, do not bind/enable, etc. */
+ if (sm->socket_name == 0)
+ return 0;
+
+#define _(N,n) do { \
+ c->id = VL_API_##N; \
+ c->name = #n; \
+ c->handler = vl_api_##n##_t_handler; \
+ c->cleanup = vl_noop_handler; \
+ c->endian = vl_api_##n##_t_endian; \
+ c->print = vl_api_##n##_t_print; \
+ c->size = sizeof(vl_api_##n##_t); \
+ c->traced = 1; /* trace, so these msgs print */ \
+ c->replay = 0; /* don't replay client create/delete msgs */ \
+ c->message_bounce = 0; /* don't bounce this message */ \
+ vl_msg_api_config(c);} while (0);
+
+ foreach_vlib_api_msg;
+#undef _
+
+ vec_resize (sm->input_buffer, 4096);
+
+ sock->config = (char *) sm->socket_name;
+
+ /* mkdir of file socket, only under /run */
+ if (strncmp (sock->config, "/run", 4) == 0)
{
- close (sockfd);
- return clib_error_return_unix (0, "listen");
+ u8 *tmp = format (0, "%s", sock->config);
+ int i = vec_len (tmp);
+ while (i && tmp[--i] != '/')
+ ;
+
+ tmp[i] = 0;
+
+ if (i)
+ vlib_unix_recursive_mkdir ((char *) tmp);
+ vec_free (tmp);
}
- pool_get (socket_main.registration_pool, rp);
+ sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
+ CLIB_SOCKET_F_ALLOW_GROUP_WRITE;
+ error = clib_socket_init (sock);
+ if (error)
+ return error;
+
+ pool_get (sm->registration_pool, rp);
memset (rp, 0, sizeof (*rp));
rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
template.read_function = socksvr_accept_ready;
template.write_function = socksvr_bogus_write;
- template.file_descriptor = sockfd;
- template.private_data = rp - socket_main.registration_pool;
+ template.file_descriptor = sock->fd;
+ template.private_data = rp - sm->registration_pool;
rp->clib_file_index = clib_file_add (fm, &template);
return 0;
socket_exit (vlib_main_t * vm)
{
clib_file_main_t *fm = &file_main;
+ socket_main_t *sm = &socket_main;
vl_api_registration_t *rp;
/* Defensive driving in case something wipes out early */
- if (socket_main.registration_pool)
+ if (sm->registration_pool)
{
u32 index;
/* *INDENT-OFF* */
- pool_foreach (rp, socket_main.registration_pool, ({
+ pool_foreach (rp, sm->registration_pool, ({
clib_file_del (fm, fm->file_pool + rp->clib_file_index);
index = rp->vl_api_registration_pool_index;
vl_free_socket_registration_index (index);
static clib_error_t *
socksvr_config (vlib_main_t * vm, unformat_input_t * input)
{
- int portno;
+ socket_main_t *sm = &socket_main;
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
- if (unformat (input, "port %d", &portno))
+ if (unformat (input, "socket-name %s", &sm->socket_name))
+ ;
+ else if (unformat (input, "default"))
{
- socket_main.portno = portno;
+ sm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
}
else
{
format_unformat_error, input);
}
}
- return socksvr_api_init (vm);
+ return 0;
}
VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
-/* argument in host byte order */
-void
-socksvr_set_port (u16 port)
-{
- socket_main.portno = port;
-}
-
-/* argument in host byte order */
-void
-socksvr_set_bind_address (u32 bind_address)
-{
- socket_main.bind_address = bind_address;
-}
-
clib_error_t *
vlibsocket_init (vlib_main_t * vm)
{
#include <netinet/in.h>
#include <netdb.h>
-#define SOCKCLNT_SERVER_PORT 32741 /* whatever */
+#define SOCKCLNT_SERVER_PORT 1234 /* whatever */
typedef signed char i8;
typedef signed short i16;
memset (buffer, 0, sizeof (buffer));
mp = (vl_api_sockclnt_create_t *) buffer;
- mp->_vl_msg_id = ntohs (8); /* VL_API_SOCKCLNT_CREATE */
+ mp->_vl_msg_id = ntohs (13); /* VL_API_SOCKCLNT_CREATE */
strncpy ((char *) mp->name, "socket-test", sizeof (mp->name) - 1);
mp->name[sizeof (mp->name) - 1] = 0;
mp->context = 0xfeedface;
rp = (vl_api_sockclnt_create_reply_t *) (buffer + 4);
/* VL_API_SOCKCLNT_CREATE_REPLY */
- if (ntohs (rp->_vl_msg_id) != 9)
+ if (ntohs (rp->_vl_msg_id) != 14)
{
printf ("WARNING: msg id %d\n", ntohs (rp->_vl_msg_id));
}
+++ /dev/null
-/* Hey Emacs use -*- mode: C -*- */
-
-/*
- *------------------------------------------------------------------
- * sockclnt.api - API message(s) to hook up clients, pass traffic
- * to client processes via TCP sockets
- *
- * Copyright (c) 2009 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *------------------------------------------------------------------
- */
-
-/*
- * Create a socket client registration
- */
-define sockclnt_create {
- u8 name[64]; /* for show, find by name, whatever */
- u32 context; /* opaque value to be returned in the reply */
-};
-
-define sockclnt_create_reply {
- i32 response; /* Non-negative = success */
- u64 handle; /* handle by which vlib knows this client */
- u32 index; /* index, used e.g. by API trace replay */
- u32 context; /* opaque value from the create request */
-};
-
-/*
- * Delete a client registration
- */
-define sockclnt_delete {
- u32 index; /* index, used e.g. by API trace replay */
- u64 handle; /* handle by which vlib knows this client */
-};
-
-define sockclnt_delete_reply {
- i32 response; /* Non-negative = success */
- u64 handle; /* in case the client wonders */
-};
-
#include <sys/stat.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
-#include <vlibsocket/vl_socket_msg_enum.h>
+
+#include <vlibmemory/vl_memory_msg_enum.h>
#define vl_typedefs /* define message structures */
-#include <vlibsocket/vl_socket_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
#undef vl_typedefs
/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
-#include <vlibsocket/vl_socket_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
#undef vl_printfun
/* instantiate all the endian swap functions we know about */
#define vl_endianfun
-#include <vlibsocket/vl_socket_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
#undef vl_endianfun
static void
+++ /dev/null
-/*
- *------------------------------------------------------------------
- * vl_socket_api_h.h - all API headers, in a specific order.
- *
- * Copyright (c) 2009 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *------------------------------------------------------------------
- */
-
-/*
- * Add to the bottom of the #include list, or elves will steal your
- * keyboard in the middle of the night!
- */
-#include <vlibmemory/vl_memory_api_h.h>
-#include <vlibsocket/sockclnt.api.h>
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */
+++ /dev/null
-/*
- *------------------------------------------------------------------
- * vl_msg_enum.h - Our view of how to number API messages
- * Clients have their own view, which has to agree with ours.
- *
- * Copyright (c) 2009 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *------------------------------------------------------------------
- */
-
-#ifndef __VL_MSG_ENUM_H__
-#define __VL_MSG_ENUM_H__
-
-#include <vppinfra/byte_order.h>
-
-#define vl_msg_id(n,h) n,
-typedef enum
-{
- VL_ILLEGAL_MESSAGE_ID = 0,
-#include <vlibsocket/vl_socket_api_h.h>
-} vl_msg_id_t;
-#undef vl_msg_id
-
-#endif /* __VL_MSG_ENUM_H__ */
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */
*/
if (0 == memcmp (e->ethernet_address,
a->ethernet, sizeof (e->ethernet_address)))
- return -1;
+ goto check_customers;
/* Update time stamp and ethernet address. */
clib_memcpy (e->ethernet_address, a->ethernet,
adj_nbr_walk_nh4 (sw_if_index, &e->ip4_address, arp_mk_complete_walk, e);
+check_customers:
/* Customer(s) waiting for this address to be resolved? */
p = hash_get (am->pending_resolutions_by_address, a->ip4.as_u32);
if (p)
* removing an etry that does not exist.
* i'll allow it, but i won't like it.
*/
- clib_warning("%U not in FIB", format_fib_prefix, prefix);
+ if (0)
+ clib_warning("%U not in FIB", format_fib_prefix, prefix);
}
else
{
static void
send_sw_interface_details (vpe_api_main_t * am,
- unix_shared_memory_queue_t * q,
+ vl_api_registration_t * rp,
vnet_sw_interface_t * swif,
u8 * interface_name, u32 context)
{
if (tag)
strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
- vl_msg_api_send_shmem (q, (u8 *) & mp);
+ vl_msg_api_send (rp, (u8 *) mp);
}
static void
vpe_api_main_t *am = &vpe_api_main;
vnet_sw_interface_t *swif;
vnet_interface_main_t *im = &am->vnet_main->interface_main;
+ vl_api_registration_t *rp;
- unix_shared_memory_queue_t *q =
- vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
+ rp = vl_api_client_index_to_registration (mp->client_index);
+
+ if (rp == 0)
+ {
+ clib_warning ("Client %d AWOL", mp->client_index);
+ return;
+ }
u8 *filter = 0, *name = 0;
if (mp->name_filter_valid)
if (filter && !strcasestr((char *) name, (char *) filter))
continue;
- send_sw_interface_details (am, q, swif, name, mp->context);
+ send_sw_interface_details (am, rp, swif, name, mp->context);
}));
/* *INDENT-ON* */
sw_if_index);
}
- ip46_address_t nh = {
- .ip4 = *dst,
- };
-
- ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
- VNET_LINK_IP4, &nh, sw_if_index);
- adj = adj_get (ai);
-
h = vlib_packet_template_get_packet (vm,
&im->ip4_arp_request_packet_template,
&bi);
vnet_buffer (b)->sw_if_index[VLIB_RX] =
vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
+ ip46_address_t nh = {
+ .ip4 = *dst,
+ };
+
+ ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
+ VNET_LINK_IP4, &nh, sw_if_index);
+ adj = adj_get (ai);
+
+ /* Peer has been previously resolved, retrieve glean adj instead */
+ if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
+ {
+ adj_unlock (ai);
+ ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP4, sw_if_index, &nh);
+ adj = adj_get (ai);
+ }
+
/* Add encapsulation string for software interface (e.g. ethernet header). */
vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
VNET_LINK_IP6, &nh, sw_if_index);
adj = adj_get (ai);
+ /* Peer has been previously resolved, retrieve glean adj instead */
+ if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
+ {
+ adj_unlock (ai);
+ ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6, sw_if_index, &nh);
+ adj = adj_get (ai);
+ }
+
vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
*/
if (0 == memcmp (n->link_layer_address,
link_layer_address, n_bytes_link_layer_address))
- return -1;
+ goto check_customers;
clib_memcpy (n->link_layer_address,
link_layer_address, n_bytes_link_layer_address);
adj_nbr_walk_nh6 (sw_if_index,
&n->key.ip6_address, ip6_nd_mk_complete_walk, n);
+check_customers:
/* Customer(s) waiting for this address to be resolved? */
p = mhash_get (&nm->pending_resolutions_by_address, a);
if (p)
}
else
{
- clib_warning ("MAC event to pid %d queue stuffed!"
- " %d MAC entries lost", client,
- evt_idx);
+ if (q)
+ clib_warning ("MAC event to pid %d queue stuffed!"
+ " %d MAC entries lost", client,
+ evt_idx);
}
evt_idx = 0;
}
}
else
{
- clib_warning ("MAC event to pid %d queue stuffed!"
- " %d MAC entries lost", client, evt_idx);
+ if (q)
+ clib_warning ("MAC event to pid %d queue stuffed!"
+ " %d MAC entries lost", client, evt_idx);
vl_msg_api_free (mp);
}
}
mapping_t *old_map;
u32 mi;
+ memset (ls_args, 0, sizeof (ls_args[0]));
memset (m_args, 0, sizeof (m_args[0]));
if (res_map_index)
res_map_index[0] = ~0;
/* find locator-set */
if (a->local)
{
+ ASSERT (a->name);
p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
}
else
memset (a, 0, sizeof (a[0]));
+ mp->locator_set_name[63] = 0;
locator_name = format (0, "%s", mp->locator_set_name);
a->name = locator_name;
locator.local = 1;
vec_add1 (locators, locator);
+ mp->locator_set_name[63] = 0;
locator_name = format (0, "%s", mp->locator_set_name);
a->name = locator_name;
if (rv)
goto out;
+ mp->locator_set_name[63] = 0;
name = format (0, "%s", mp->locator_set_name);
p = hash_get_mem (lcm->locator_set_index_by_name, name);
if (!p)
u8 *locator_set_name = NULL;
vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
+ mp->locator_set_name[63] = 0;
locator_set_name = format (0, "%s", mp->locator_set_name);
a->is_add = mp->is_add;
if (!mp->is_add)
{
vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
+ memset (a, 0, sizeof (*a));
gid_address_copy (&a->reid, eid);
a->is_add = 0;
rv = vnet_lisp_add_del_adjacency (a);
rv = vnet_lisp_add_mapping (m_args, rlocs, NULL, NULL);
}
else
- rv = vnet_lisp_del_mapping (eid, NULL);
+ {
+ rv = vnet_lisp_del_mapping (eid, NULL);
+ }
if (mp->del_all)
vnet_lisp_clear_all_remote_adjacencies ();
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vpp/app/version.h>
#define TCP_BUILTIN_CLIENT_DBG (0)
vac_rx_thread_fn (void *arg)
{
unix_shared_memory_queue_t *q;
+ vl_api_memclnt_keepalive_t *mp;
+ vl_api_memclnt_keepalive_reply_t *rmp;
vac_main_t *pm = &vac_main;
api_main_t *am = &api_main;
+ vl_shmem_hdr_t *shmem_hdr;
uword msg;
q = am->vl_input_queue;
vl_msg_api_free((void *) msg);
break;
+ case VL_API_MEMCLNT_KEEPALIVE:
+ mp = (void *)msg;
+ rmp = vl_msg_api_alloc (sizeof (*rmp));
+ memset (rmp, 0, sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
+ rmp->context = mp->context;
+ shmem_hdr = am->shmem_hdr;
+ vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
+ vl_msg_api_free((void *) msg);
+ break;
+
default:
vac_api_handler((void *)msg);
}
unix_shared_memory_queue_t *q;
api_main_t *am = &api_main;
vac_main_t *pm = &vac_main;
+ vl_api_memclnt_keepalive_t *mp;
+ vl_api_memclnt_keepalive_reply_t *rmp;
uword msg;
msgbuf_t *msgbuf;
+ int rv;
+ vl_shmem_hdr_t *shmem_hdr;
if (!pm->connected_to_vlib) return -1;
set_timeout(timeout);
q = am->vl_input_queue;
- int rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0);
+
+ again:
+ rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0);
if (rv == 0) {
u16 msg_id = ntohs(*((u16 *)msg));
switch (msg_id) {
case VL_API_MEMCLNT_READ_TIMEOUT:
printf("Received read timeout %ds\n", timeout);
goto error;
+ case VL_API_MEMCLNT_KEEPALIVE:
+ /* Handle an alive-check ping from vpp. */
+ mp = (void *)msg;
+ rmp = vl_msg_api_alloc (sizeof (*rmp));
+ memset (rmp, 0, sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
+ rmp->context = mp->context;
+ shmem_hdr = am->shmem_hdr;
+ vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
+ vl_msg_api_free((void *) msg);
+ /*
+ * Python code is blissfully unaware of these pings, so
+ * act as if it never happened...
+ */
+ goto again;
default:
msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
#include <vpp-api/vapi/vapi.h>
#include <vpp-api/vapi/vapi_internal.h>
#include <vppinfra/types.h>
+#include <vppinfra/pool.h>
+#include <vlib/vlib.h>
#include <vlibapi/api_common.h>
#include <vlibmemory/api_common.h>
{
vl_api_cli_inband_reply_t *rmp;
int rv = 0;
- unix_shared_memory_queue_t *q;
vlib_main_t *vm = vlib_get_main ();
unformat_input_t input;
u8 *out_vec = 0;
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (!q)
- return;
-
unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
char *vpe_api_get_version (void);
char *vpe_api_get_build_date (void);
- unix_shared_memory_queue_t *q =
- vl_api_client_index_to_input_queue (mp->client_index);
-
- if (!q)
- return;
-
/* *INDENT-OFF* */
REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
({
vam->my_client_index = (u32) ~ 0;
/* Ensure that vam->inbuf is never NULL */
vec_validate (vam->inbuf, 0);
+ vec_validate (vam->cmd_reply, 0);
+ vec_reset_length (vam->cmd_reply);
init_error_string_table (vam);
rv = vat_plugin_init (vam);
if (rv)
clib_warning ("vat_plugin_init returned %d", rv);
+
return 0;
}
vam->help_by_name = hash_create_string (0, sizeof (uword));
}
+static void
+maybe_register_api_client (vat_main_t * vam)
+{
+ vl_api_registration_t **regpp;
+ vl_api_registration_t *regp;
+ svm_region_t *svm;
+ void *oldheap;
+ api_main_t *am = &api_main;
+
+ if (vam->my_client_index != ~0)
+ return;
+
+ pool_get (am->vl_clients, regpp);
+
+ svm = am->vlib_rp;
+
+ pthread_mutex_lock (&svm->mutex);
+ oldheap = svm_push_data_heap (svm);
+ *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
+
+ regp = *regpp;
+ memset (regp, 0, sizeof (*regp));
+ regp->registration_type = REGISTRATION_TYPE_SHMEM;
+ regp->vl_api_registration_pool_index = regpp - am->vl_clients;
+ regp->vlib_rp = svm;
+ regp->shmem_hdr = am->shmem_hdr;
+
+ /* Loopback connection */
+ regp->vl_input_queue = am->shmem_hdr->vl_input_queue;
+
+ regp->name = format (0, "%s", "vpp-internal");
+ vec_add1 (regp->name, 0);
+
+ pthread_mutex_unlock (&svm->mutex);
+ svm_pop_heap (oldheap);
+
+ vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
+ (regp->vl_api_registration_pool_index,
+ am->shmem_hdr->application_restarts);
+}
+
static clib_error_t *
api_command_fn (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cmd)
int (*fp) (vat_main_t *);
api_main_t *am = &api_main;
+ maybe_register_api_client (vam);
+
vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
/* vec_validated in the init routine */
FINISH;
}
+static void *vl_api_sockclnt_create_t_print
+ (vl_api_sockclnt_create_t * mp, void *handle)
+{
+ u8 *s;
+
+ s = format (0, "SCRIPT: sockclnt_create name %s ", mp->name);
+
+ FINISH;
+}
+
static void *vl_api_show_version_t_print
(vl_api_show_version_t * mp, void *handle)
{
_(CLI, cli) \
_(CLI_INBAND, cli_inband) \
_(MEMCLNT_CREATE, memclnt_create) \
+_(SOCKCLNT_CREATE, sockclnt_create) \
_(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump) \
_(SHOW_VERSION, show_version) \
_(L2_FIB_TABLE_DUMP, l2_fib_table_dump) \
#ifdef CLIB_UNIX
always_inline clib_error_t *
-elog_write_file (elog_main_t * em, char *unix_file, int flush_ring)
+elog_write_file (elog_main_t * em, char *clib_file, int flush_ring)
{
serialize_main_t m;
clib_error_t *error;
- error = serialize_open_unix_file (&m, unix_file);
+ error = serialize_open_clib_file (&m, clib_file);
if (error)
return error;
error = serialize (&m, serialize_elog_main, em, flush_ring);
}
always_inline clib_error_t *
-elog_read_file (elog_main_t * em, char *unix_file)
+elog_read_file (elog_main_t * em, char *clib_file)
{
serialize_main_t m;
clib_error_t *error;
- error = unserialize_open_unix_file (&m, unix_file);
+ error = unserialize_open_clib_file (&m, clib_file);
if (error)
return error;
error = unserialize (&m, unserialize_elog_main, em);
UNIX_FILE_UPDATE_ADD,
UNIX_FILE_UPDATE_MODIFY,
UNIX_FILE_UPDATE_DELETE,
-} unix_file_update_type_t;
+} clib_file_update_type_t;
typedef struct
{
clib_file_t *file_pool;
void (*file_update) (clib_file_t * file,
- unix_file_update_type_t update_type);
+ clib_file_update_type_t update_type);
} clib_file_main_t;
/* Unix specific formats. */
#ifdef CLIB_UNIX
/* Setup input from Unix file. */
-void unformat_init_unix_file (unformat_input_t * input, int file_descriptor);
+void unformat_init_clib_file (unformat_input_t * input, int file_descriptor);
/* Take input from Unix environment variable; returns
1 if variable exists zero otherwise. */
#include <fcntl.h>
static void
-unix_file_write (serialize_main_header_t * m, serialize_stream_t * s)
+clib_file_write (serialize_main_header_t * m, serialize_stream_t * s)
{
int fd, n;
}
static void
-unix_file_read (serialize_main_header_t * m, serialize_stream_t * s)
+clib_file_read (serialize_main_header_t * m, serialize_stream_t * s)
{
int fd, n;
}
static void
-serialize_open_unix_file_descriptor_helper (serialize_main_t * m, int fd,
+serialize_open_clib_file_descriptor_helper (serialize_main_t * m, int fd,
uword is_read)
{
memset (m, 0, sizeof (m[0]));
_vec_len (m->stream.buffer) = 0;
}
- m->header.data_function = is_read ? unix_file_read : unix_file_write;
+ m->header.data_function = is_read ? clib_file_read : clib_file_write;
m->stream.data_function_opaque = fd;
}
void
-serialize_open_unix_file_descriptor (serialize_main_t * m, int fd)
+serialize_open_clib_file_descriptor (serialize_main_t * m, int fd)
{
- serialize_open_unix_file_descriptor_helper (m, fd, /* is_read */ 0);
+ serialize_open_clib_file_descriptor_helper (m, fd, /* is_read */ 0);
}
void
-unserialize_open_unix_file_descriptor (serialize_main_t * m, int fd)
+unserialize_open_clib_file_descriptor (serialize_main_t * m, int fd)
{
- serialize_open_unix_file_descriptor_helper (m, fd, /* is_read */ 1);
+ serialize_open_clib_file_descriptor_helper (m, fd, /* is_read */ 1);
}
static clib_error_t *
-serialize_open_unix_file_helper (serialize_main_t * m, char *file,
+serialize_open_clib_file_helper (serialize_main_t * m, char *file,
uword is_read)
{
int fd, mode;
if (fd < 0)
return clib_error_return_unix (0, "open `%s'", file);
- serialize_open_unix_file_descriptor_helper (m, fd, is_read);
+ serialize_open_clib_file_descriptor_helper (m, fd, is_read);
return 0;
}
clib_error_t *
-serialize_open_unix_file (serialize_main_t * m, char *file)
+serialize_open_clib_file (serialize_main_t * m, char *file)
{
- return serialize_open_unix_file_helper (m, file, /* is_read */ 0);
+ return serialize_open_clib_file_helper (m, file, /* is_read */ 0);
}
clib_error_t *
-unserialize_open_unix_file (serialize_main_t * m, char *file)
+unserialize_open_clib_file (serialize_main_t * m, char *file)
{
- return serialize_open_unix_file_helper (m, file, /* is_read */ 1);
+ return serialize_open_clib_file_helper (m, file, /* is_read */ 1);
}
#endif /* CLIB_UNIX */
void unserialize_open_vector (serialize_main_t * m, u8 * vector);
#ifdef CLIB_UNIX
-clib_error_t *serialize_open_unix_file (serialize_main_t * m, char *file);
-clib_error_t *unserialize_open_unix_file (serialize_main_t * m, char *file);
+clib_error_t *serialize_open_clib_file (serialize_main_t * m, char *file);
+clib_error_t *unserialize_open_clib_file (serialize_main_t * m, char *file);
-void serialize_open_unix_file_descriptor (serialize_main_t * m, int fd);
-void unserialize_open_unix_file_descriptor (serialize_main_t * m, int fd);
+void serialize_open_clib_file_descriptor (serialize_main_t * m, int fd);
+void unserialize_open_clib_file_descriptor (serialize_main_t * m, int fd);
#endif /* CLIB_UNIX */
/* Main routines. */
#ifdef CLIB_UNIX
if (tm->dump_file)
- serialize_open_unix_file (sm, tm->dump_file);
+ serialize_open_clib_file (sm, tm->dump_file);
else
#endif
serialize_open_vector (sm, 0);
#ifdef CLIB_UNIX
if (tm->dump_file)
{
- if ((error = unserialize_open_unix_file (um, tm->dump_file)))
+ if ((error = unserialize_open_clib_file (um, tm->dump_file)))
goto done;
}
else
if (fd < 0)
return cpu_freq;
- unformat_init_unix_file (&input, fd);
+ unformat_init_clib_file (&input, fd);
ppc_timebase = 0;
while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
if (fd < 0)
goto done;
- unformat_init_unix_file (&input, fd);
+ unformat_init_clib_file (&input, fd);
unformat (&input, "%f", &cpu_freq);
cpu_freq *= 1e3; /* measured in kHz */
unformat_free (&input);
#ifdef CLIB_UNIX
static uword
-unix_file_fill_buffer (unformat_input_t * input)
+clib_file_fill_buffer (unformat_input_t * input)
{
int fd = pointer_to_uword (input->fill_buffer_arg);
uword l, n;
}
void
-unformat_init_unix_file (unformat_input_t * input, int file_descriptor)
+unformat_init_clib_file (unformat_input_t * input, int file_descriptor)
{
- unformat_init (input, unix_file_fill_buffer,
+ unformat_init (input, clib_file_fill_buffer,
uword_to_pointer (file_descriptor, void *));
}
__thread uword __os_thread_index = 0;
clib_error_t *
-unix_file_n_bytes (char *file, uword * result)
+clib_file_n_bytes (char *file, uword * result)
{
struct stat s;
}
clib_error_t *
-unix_file_read_contents (char *file, u8 * result, uword n_bytes)
+clib_file_read_contents (char *file, u8 * result, uword n_bytes)
{
int fd = -1;
uword n_done, n_left;
}
clib_error_t *
-unix_file_contents (char *file, u8 ** result)
+clib_file_contents (char *file, u8 ** result)
{
uword n_bytes;
clib_error_t *error = 0;
u8 *v;
- if ((error = unix_file_n_bytes (file, &n_bytes)))
+ if ((error = clib_file_n_bytes (file, &n_bytes)))
return error;
v = 0;
vec_resize (v, n_bytes);
- error = unix_file_read_contents (file, v, n_bytes);
+ error = clib_file_read_contents (file, v, n_bytes);
if (error)
vec_free (v);
#include <vppinfra/error.h>
/* Number of bytes in a Unix file. */
-clib_error_t *unix_file_n_bytes (char *file, uword * result);
+clib_error_t *clib_file_n_bytes (char *file, uword * result);
/* Read file contents into given buffer. */
-clib_error_t *unix_file_read_contents (char *file, u8 * result,
+clib_error_t *clib_file_read_contents (char *file, u8 * result,
uword n_bytes);
/* Read and return contents of Unix file. */
-clib_error_t *unix_file_contents (char *file, u8 ** result);
+clib_error_t *clib_file_contents (char *file, u8 ** result);
/* As above but for /proc file system on Linux. */
clib_error_t *unix_proc_file_contents (char *file, u8 ** result);