/* *------------------------------------------------------------------ * api_shared.c - API message handling, common code for both clients * and the vlib process itself. * * * 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. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include /* *INDENT-OFF* */ api_main_t api_main = { .region_name = "/unset", .api_uid = -1, .api_gid = -1, }; /* *INDENT-ON* */ void vl_msg_api_increment_missing_client_counter (void) { api_main_t *am = &api_main; am->missing_clients++; } int vl_msg_api_rx_trace_enabled (api_main_t * am) { return (am->rx_trace && am->rx_trace->enabled); } int vl_msg_api_tx_trace_enabled (api_main_t * am) { return (am->tx_trace && am->tx_trace->enabled); } /* * vl_msg_api_trace */ void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg) { u8 **this_trace; u8 **old_trace; u8 *msg_copy; u32 length; trace_cfg_t *cfgp; u16 msg_id = ntohs (*((u16 *) msg)); msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data)); cfgp = am->api_trace_cfg + msg_id; if (!cfgp || !cfgp->trace_enable) return; msg_copy = 0; if (tp->nitems == 0) { clib_warning ("tp->nitems is 0"); return; } if (vec_len (tp->traces) < tp->nitems) { vec_add1 (tp->traces, 0); this_trace = tp->traces + vec_len (tp->traces) - 1; } else { tp->wrapped = 1; old_trace = tp->traces + tp->curindex++; if (tp->curindex == tp->nitems) tp->curindex = 0; vec_free (*old_trace); this_trace = old_trace; } length = clib_net_to_host_u32 (header->data_len); vec_validate (msg_copy, length - 1); clib_memcpy (msg_copy, msg, length); *this_trace = msg_copy; } int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which, int onoff) { vl_api_trace_t *tp; int rv; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; if (tp == 0) { vl_msg_api_trace_configure (am, which, 1024); tp = am->tx_trace; } break; case VL_API_TRACE_RX: tp = am->rx_trace; if (tp == 0) { vl_msg_api_trace_configure (am, which, 1024); tp = am->rx_trace; } break; default: /* duh? */ return -1; } /* Configured? */ if (tp == 0 || tp->nitems == 0) return -1; rv = tp->enabled; tp->enabled = onoff; return rv; } int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which) { vl_api_trace_t *tp; int i; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; break; case VL_API_TRACE_RX: tp = am->rx_trace; break; default: /* duh? */ return -1; } /* Configured? */ if (!tp || tp->nitems == 0) return -1; tp->curindex = 0; tp->wrapped = 0; for (i = 0; i < vec_len (tp->traces); i++) { vec_free (tp->traces[i]); } vec_free (tp->traces); return 0; } int vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp) { vl_api_trace_t *tp; vl_api_trace_file_header_t fh; int i; u8 *msg; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; break; case VL_API_TRACE_RX: tp = am->rx_trace; break; default: /* duh? */ return -1; } /* Configured, data present? */ if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0) return -1; /* "Dare to be stupid" check */ if (fp == 0) { return -2; } /* Write the file header */ fh.nitems = vec_len (tp->traces); fh.endian = tp->endian; fh.wrapped = tp->wrapped; if (fwrite (&fh, sizeof (fh), 1, fp) != 1) { return (-10); } /* No-wrap case */ if (tp->wrapped == 0) { /* * Note: vec_len return 0 when fed a NULL pointer. * Unfortunately, the static analysis tool doesn't * figure it out, hence the suppressed warnings. * What a great use of my time. */ for (i = 0; i < vec_len (tp->traces); i++) { u32 msg_length; /*sa_ignore NO_NULL_CHK */ msg = tp->traces[i]; /* * This retarded check required to pass * [sic] SA-checking. */ if (!msg) continue; msg_length = clib_host_to_net_u32 (vec_len (msg)); if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) { return (-14); } if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) { return (-11); } } } else { /* Wrap case: write oldest -> end of buffer */ for (i = tp->curindex; i < vec_len (tp->traces); i++) { u32 msg_length; msg = tp->traces[i]; /* * This retarded check required to pass * [sic] SA-checking */ if (!msg) continue; msg_length = clib_host_to_net_u32 (vec_len (msg)); if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) { return (-14); } if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) { return (-12); } } /* write beginning of buffer -> oldest-1 */ for (i = 0; i < tp->curindex; i++) { u32 msg_length; /*sa_ignore NO_NULL_CHK */ msg = tp->traces[i]; /* * This retarded check required to pass * [sic] SA-checking */ if (!msg) continue; msg_length = clib_host_to_net_u32 (vec_len (msg)); if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length)) { return (-14); } if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg)) { return (-13); } } } return 0; } int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which, u32 nitems) { vl_api_trace_t *tp; int was_on = 0; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; if (tp == 0) {
/*
 *------------------------------------------------------------------
 * vl_memory_api_h.h - memory API headers, in a specific order.
 *
 * Copyright (c) 2009-2010 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 current layer (2) last, or an artistic disagreement
 * about message numbering will occur
 */

#ifndef included_from_layer_3
#include <vlibmemory/vl_memory_api_h.h>
#endif /* included_from_layer_3 */

#include <vnet/devices/af_packet/af_packet.api.h>
#include <vnet/devices/netmap/netmap.api.h>
#include <vnet/devices/virtio/vhost_user.api.h>
#include <vnet/gre/gre.api.h>
#include <vnet/interface.api.h>
#include <vnet/map/map.api.h>
#include <vnet/l2/l2.api.h>
#include <vnet/l2tp/l2tp.api.h>
#include <vnet/span/span.api.h>
#include <vnet/ip/ip.api.h>
#include <vnet/unix/tap.api.h>
#include <vnet/vxlan/vxlan.api.h>
#include <vnet/lldp/lldp.api.h>
#include <vnet/vxlan-gpe/vxlan_gpe.api.h>
#include <vnet/bfd/bfd.api.h>
#include <vnet/ipsec/ipsec.api.h>
#include <vnet/ipsec-gre/ipsec_gre.api.h>
#include <vnet/lisp-cp/lisp.api.h>
#include <vnet/lisp-gpe/lisp_gpe.api.h>
#include <vnet/lisp-cp/one.api.h>
#include <vnet/session/session.api.h>
#include <vnet/mpls/mpls.api.h>
#include <vnet/srv6/sr.api.h>
#include <vnet/classify/classify.api.h></