From 38bbf1ef06bedfb4b9058afcf6685c77538818a6 Mon Sep 17 00:00:00 2001 From: Vratko Polak Date: Thu, 1 Aug 2019 13:50:48 +0200 Subject: Fix CRCs, bump stable VPP version + Migrate the data to a separate yaml file. + Improve some argument names. + Unify handling of unicode (to always utf-8 encode to str). Change-Id: Id0622b24202be796c1cd33ad52c3b8dca81cff50 Signed-off-by: Vratko Polak --- resources/libraries/python/VppApiCrc.py | 233 +++++++------------------------- 1 file changed, 50 insertions(+), 183 deletions(-) (limited to 'resources/libraries/python/VppApiCrc.py') diff --git a/resources/libraries/python/VppApiCrc.py b/resources/libraries/python/VppApiCrc.py index 28d66d3cd4..0d4e8f4f0c 100644 --- a/resources/libraries/python/VppApiCrc.py +++ b/resources/libraries/python/VppApiCrc.py @@ -15,13 +15,29 @@ import json import os +import yaml from robot.api import logger +def _str(text): + """Convert from possible unicode without interpreting as number. + + :param text: Input to convert. + :type text: str or unicode + :returns: Converted text. + :rtype: str + """ + return text.encode("utf-8") if isinstance(text, unicode) else text + + class VppApiCrcChecker(object): """Holder of data related to tracking VPP API CRCs. + Both message names and crc hexa strings are tracked as + ordinary Python2 (bytes) str, so _str() is used when input is + possibly unicode or otherwise not safe. + Each instance of this class starts with same default state, so make sure the calling libraries have appropriate robot library scope. For usual testing, it means "GLOBAL" scope.""" @@ -40,13 +56,11 @@ class VppApiCrcChecker(object): """Mapping from collection name to mapping from API name to CRC string. Colection name should be something useful for logging. - API name is ordinary Python2 str, CRC is also str. Order of addition reflects the order colections should be queried. If an incompatible CRC is found, affected collections are removed. A CRC that would remove all does not, added to _reported instead, - while causing a failure in single test. - """ + while causing a failure in single test.""" self._missing = dict() """Mapping from collection name to mapping from API name to CRC string. @@ -75,19 +89,32 @@ class VppApiCrcChecker(object): self._register_all() self._check_dir(directory) - def _register_collection(self, collection_name, collection_dict): + def _register_collection(self, collection_name, name_to_crc_mapping): """Add a named (copy of) collection of CRCs. :param collection_name: Helpful string describing the collection. - :param collection_dict: Mapping from API names to CRCs. - :type collection_name: str - :type collection_dict: dict from str to str + :param name_to_crc_mapping: Mapping from API names to CRCs. + :type collection_name: str or unicode + :type name_to_crc_mapping: dict from str/unicode to str/unicode """ + collection_name = _str(collection_name) if collection_name in self._expected: - raise RuntimeError("Collection {cl} already registered.".format( + raise RuntimeError("Collection {cl!r} already registered.".format( cl=collection_name)) - self._expected[collection_name] = collection_dict.copy() - self._missing[collection_name] = collection_dict.copy() + mapping = {_str(k): _str(v) for k, v in name_to_crc_mapping.items()} + self._expected[collection_name] = mapping + self._missing[collection_name] = mapping.copy() + + def _register_all(self): + """Add all collections this CSIT codebase is tested against.""" + + file_path = os.path.normpath(os.path.join( + os.path.dirname(os.path.abspath(__file__)), "..", "..", + "api", "vpp", "supported_crcs.yaml")) + with open(file_path, "r") as file_in: + collections_dict = yaml.load(file_in.read()) + for collection_name, name_to_crc_mapping in collections_dict.items(): + self._register_collection(collection_name, name_to_crc_mapping) @staticmethod def _get_name(msg_obj): @@ -102,7 +129,7 @@ class VppApiCrcChecker(object): for item in msg_obj: if isinstance(item, (dict, list)): continue - return item + return _str(item) raise RuntimeError("No name found for message: {obj!r}".format( obj=msg_obj)) @@ -121,7 +148,7 @@ class VppApiCrcChecker(object): continue crc = item.get("crc", None) if crc: - return crc + return _str(crc) raise RuntimeError("No CRC found for message: {obj!r}".format( obj=msg_obj)) @@ -153,16 +180,16 @@ class VppApiCrcChecker(object): :param api_name: API name to check. :param crc: Discovered CRC to check for the name. :type api_name: str - :type crc: str or unicode + :type crc: str """ # Regardless of the result, remember as found. self._found[api_name] = crc old_expected = self._expected new_expected = old_expected.copy() - for collection_name, collection_dict in old_expected.items(): - if api_name not in collection_dict: + for collection_name, name_to_crc_mapping in old_expected.items(): + if api_name not in name_to_crc_mapping: continue - if collection_dict[api_name] == crc: + if name_to_crc_mapping[api_name] == crc: self._missing[collection_name].pop(api_name, None) continue # Remove the offending collection. @@ -201,7 +228,7 @@ class VppApiCrcChecker(object): msg_name = self._get_name(msg_obj) msg_crc = self._get_crc(msg_obj) self._process_crc(msg_name, msg_crc) - logger.info("Surviving collections: {col}".format( + logger.debug("Surviving collections: {col!r}".format( col=self._expected.keys())) def report_initial_conflicts(self, report_missing=False): @@ -239,15 +266,16 @@ class VppApiCrcChecker(object): Intended use: Call everytime an API call is queued or response received. :param api_name: VPP API messagee name to check. - :type api_name: str + :type api_name: str or unicode :raises RuntimeError: If no verified CRC for the api_name is found. """ + api_name = _str(api_name) if api_name in self._reported: return old_expected = self._expected new_expected = old_expected.copy() - for collection_name, collection_dict in old_expected.items(): - if api_name in collection_dict: + for collection_name, name_to_crc_mapping in old_expected.items(): + if api_name in name_to_crc_mapping: continue # Remove the offending collection. new_expected.pop(collection_name, None) @@ -258,166 +286,5 @@ class VppApiCrcChecker(object): crc = self._found.get(api_name, None) self._reported[api_name] = crc # Disabled temporarily during CRC mismatch. - #raise RuntimeError("No active collection has API {api} CRC found {crc}"\ - # .format(api=api_name, crc=crc)) - - # Moved to the end as this part will be edited frequently. - def _register_all(self): - """Add all collections this CSIT codebase is tested against.""" - - # Rework to read from files? - self._register_collection( - "19.08-rc0~762-gbb2e5221a", { - "acl_add_replace": "0x13bc8539", # perf - "acl_add_replace_reply": "0xac407b0c", # perf - "acl_dump": "0xef34fea4", # perf teardown - "acl_interface_list_dump": "0x529cb13f", # perf teardown - # ^^^^ tc01-64B-1c-ethip4udp-ip4base-iacl1sf-10kflows-mrr - "acl_interface_set_acl_list": "0x8baece38", # perf - "acl_interface_set_acl_list_reply": "0xe8d4e804", # perf - "acl_details": "0xf89d7a88", # perf teardown - "acl_interface_list_details": "0xd5e80809", # perf teardown - # ^^^^ tc01-64B-1c-ethip4udp-ip4base-iacl1sl-10kflows-mrr - # ^^ ip4fwdANDiaclANDacl10AND100_flows - "avf_create": "0xdaab8ae2", # perf - "avf_create_reply": "0xfda5941f", # perf - # ^^ tc01-64B-1c-avf-eth-l2bdbasemaclrn-mrr - # ^ l2bdmaclrnANDbaseANDdrv_avf - "bridge_domain_add_del": "0xc6360720", # dev - "bridge_domain_add_del_reply": "0xe8d4e804", # dev - "classify_add_del_session": "0x85fd79f4", # dev - "classify_add_del_session_reply": "0xe8d4e804", # dev - "classify_add_del_table": "0x9bd794ae", # dev - "classify_add_del_table_reply": "0x05486349", # dev - "cli_inband": "0xb1ad59b3", # dev setup - "cli_inband_reply": "0x6d3c80a4", # dev setup - "cop_interface_enable_disable": "0x69d24598", # dev - "cop_interface_enable_disable_reply": "0xe8d4e804", # dev - "cop_whitelist_enable_disable": "0x8bb8f6dc", # dev - "cop_whitelist_enable_disable_reply": "0xe8d4e804", # dev - "create_loopback": "0x3b54129c", # dev - "create_loopback_reply": "0xfda5941f", # dev - "create_subif": "0x86cfe408", # virl - "create_subif_reply": "0xfda5941f", # virl - "create_vhost_user_if": "0xbd230b87", # dev - "create_vhost_user_if_reply": "0xfda5941f", # dev - "create_vlan_subif": "0x70cadeda", # virl - "create_vlan_subif_reply": "0xfda5941f", # virl - "gre_tunnel_add_del": "0x04199f47", # virl - "gre_tunnel_add_del_reply": "0x903324db", # virl - "gpe_enable_disable": "0xeb0e943b", # virl - "gpe_enable_disable_reply": "0xe8d4e804", # virl - "hw_interface_set_mtu": "0x132da1e7", # dev - "hw_interface_set_mtu_reply": "0xe8d4e804", # dev - "input_acl_set_interface": "0xe09537b0", # dev - "input_acl_set_interface_reply": "0xe8d4e804", # dev - "ip_address_details": "0x2f1dbc7d", # dev - "ip_address_dump": "0x6b7bcd0a", # dev - "ip_neighbor_add_del": "0x7a68a3c4", # dev - "ip_neighbor_add_del_reply": "0x1992deab", # dev - "ip_probe_neighbor": "0x2736142d", # virl - "ip_route_add_del": "0x83e086ce", # dev - "ip_route_add_del_reply": "0x1992deab", # dev - "ip_source_check_interface_add_del": "0x0a60152a", # virl - "ip_source_check_interface_add_del_reply": "0xe8d4e804", # virl - "ip_table_add_del": "0xe5d378f2", # dev - "ip_table_add_del_reply": "0xe8d4e804", # dev - "ipsec_interface_add_del_spd": "0x1e3b8286", # dev - "ipsec_interface_add_del_spd_reply": "0xe8d4e804", # dev - "ipsec_sad_entry_add_del": "0xa25ab61e", # dev - "ipsec_sad_entry_add_del_reply": "0x9ffac24b", # dev - "ipsec_spd_add_del": "0x9ffdf5da", # dev - "ipsec_spd_add_del_reply": "0xe8d4e804", # dev - "ipsec_spd_entry_add_del": "0x6bc6a3b5", # dev - "ipsec_spd_entry_add_del_reply": "0x9ffac24b", # dev - "l2_interface_vlan_tag_rewrite": "0xb90be6b4", # virl - "l2_interface_vlan_tag_rewrite_reply": "0xe8d4e804", # virl - "l2_patch_add_del": "0x62506e63", # perf - "l2_patch_add_del_reply": "0xe8d4e804", # perf - # ^^ tc01-64B-1c-avf-eth-l2patch-mrr - # ^ l2patchANDdrv_avf - "lisp_add_del_adjacency": "0xf047390d", # virl - "lisp_add_del_adjacency_reply": "0xe8d4e804", # virl - "lisp_add_del_local_eid": "0xe6d00717", # virl - "lisp_add_del_local_eid_reply": "0xe8d4e804", # virl - "lisp_add_del_locator": "0x006a4240", # virl - "lisp_add_del_locator_reply": "0xe8d4e804", # virl - "lisp_add_del_locator_set": "0x06968e38", # virl - "lisp_add_del_locator_set_reply": "0xb6666db4", # virl - "lisp_add_del_remote_mapping": "0xb879c3a9", # virl - "lisp_add_del_remote_mapping_reply": "0xe8d4e804", # virl - "lisp_eid_table_details": "0xdcd9f414", # virl - "lisp_eid_table_dump": "0xe0df64da", # virl - "lisp_enable_disable": "0xeb0e943b", # virl - "lisp_enable_disable_reply": "0xe8d4e804", # virl - "lisp_locator_set_details": "0x6b846882", # virl - "lisp_locator_set_dump": "0xc79e8ab0", # virl - "lisp_map_resolver_details": "0x60a5f5ca", # virl - "lisp_map_resolver_dump": "0x51077d14", # virl - "memif_create": "0x6597cdb2", # dev - "memif_create_reply": "0xfda5941f", # dev - "memif_details": "0x4f5a3397", # dev - "memif_dump": "0x51077d14", # dev - "memif_socket_filename_add_del": "0x30e3929d", # dev - "memif_socket_filename_add_del_reply": "0xe8d4e804", # dev - "nat_det_add_del_map": "0x04b76549", # perf - "nat_det_add_del_map_reply": "0xe8d4e804", # perf - "nat44_interface_add_del_feature": "0xef3edad1", # perf - "nat44_interface_add_del_feature_reply": "0xe8d4e804", # perf - # ^^^^ tc01-64B-1c-ethip4udp-ip4base-nat44-mrr - # ^ nat44NOTscaleNOTsrc_user_1 - "proxy_arp_intfc_enable_disable": "0x69d24598", # virl - "proxy_arp_intfc_enable_disable_reply": "0xe8d4e804", # virl - "show_lisp_status": "0x51077d14", # virl - "show_lisp_status_reply": "0xddcf48ef", # virl - "show_threads": "0x51077d14", # dev - "show_threads_reply": "0xf5e0b66f", # dev - "show_version": "0x51077d14", # dev setup - "show_version_reply": "0xb9bcf6df", # dev setup - "sw_interface_add_del_address": "0x7b583179", # dev - "sw_interface_add_del_address_reply": "0xe8d4e804", # dev - "sw_interface_details": "0xe4ee7eb6", # dev setup - "sw_interface_dump": "0x052753c5", # dev setup - "sw_interface_ip6nd_ra_config": "0xc3f02daa", # dev - "sw_interface_ip6nd_ra_config_reply": "0xe8d4e804", # dev - "sw_interface_rx_placement_details": "0x0e9e33f4", # perf - "sw_interface_rx_placement_dump": "0x529cb13f", # perf - # ^^ tc01-64B-1c-dot1q-l2bdbasemaclrn-eth-2memif-1dcr-mrr - # ^ dot1qANDl2bdmaclrnANDbaseANDmemif - "sw_interface_set_flags": "0x555485f5", # dev - "sw_interface_set_flags_reply": "0xe8d4e804", # dev - "sw_interface_set_l2_bridge": "0x5579f809", # dev - "sw_interface_set_l2_bridge_reply": "0xe8d4e804", # dev - "sw_interface_set_l2_xconnect": "0x95de3988", # dev - "sw_interface_set_l2_xconnect_reply": "0xe8d4e804", # dev - "sw_interface_set_rx_placement": "0x4ef4377d", # perf - "sw_interface_set_rx_placement_reply": "0xe8d4e804", # perf - # ^^ tc01-64B-1c-eth-l2xcbase-eth-2memif-1dcr-mrr - # ^ l2xcfwdANDbaseANDlxcANDmemif - "sw_interface_set_table": "0xacb25d89", # dev - "sw_interface_set_table_reply": "0xe8d4e804", # dev - "sw_interface_set_vxlan_bypass": "0xe74ca095", # dev - "sw_interface_set_vxlan_bypass_reply": "0xe8d4e804", # dev - "sw_interface_vhost_user_details": "0x91ff3307", # dev - "sw_interface_vhost_user_dump": "0x51077d14", # dev - "vxlan_add_del_tunnel": "0x00f4bdd0", # virl - "vxlan_add_del_tunnel_reply": "0xfda5941f", # virl - "vxlan_tunnel_details": "0xce38e127", # virl - "vxlan_tunnel_dump": "0x529cb13f", # virl - } - # Perf verify with: csit-3n-skx-perftest - # mrrAND1cAND64bANDnic_intel-x710ANDip4fwdANDiaclANDacl10AND100_flows - # mrrAND1cAND64bANDnic_intel-x710ANDl2bdmaclrnANDbaseANDdrv_avf - # mrrAND1cAND64bANDnic_intel-x710ANDl2patchANDdrv_avf - # mrrAND1cAND64bANDnic_intel-x710ANDnat44NOTscaleNOTsrc_user_1 - # mrrAND1cAND64bANDnic_intel-x710ANDdot1qANDl2bdmaclrnANDbaseANDmemif - # mrrAND1cAND64bANDnic_intel-x710ANDl2xcfwdANDbaseANDlxcANDmemif - - # TODO: Add a tag expression for covering those perf tests, - # even though any CSIT change can make that outdated. - # TODO: Once API coverage job is ready, - # add a check to make sure each message was encountered; - # failure means we need to add more tests to API coverage job. - # Alternatively, add an option to compile messages actually - # used or encountered, so CSIT knows what to remove from mapping. - ) + #raise RuntimeError("No active collection has API {api!r}" + # " CRC found {crc!r}".format(api=api_name, crc=crc)) -- cgit 1.2.3-korg d='n403' href='#n403'>403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/*
 *------------------------------------------------------------------
 * vlib_api.c VLIB API implementation
 *
 * 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 <fcntl.h>
#include <pthread.h>
#include <vppinfra/vec.h>
#include <vppinfra/hash.h>
#include <vppinfra/pool.h>
#include <vppinfra/format.h>
#include <vppinfra/byte_order.h>
#include <vppinfra/elog.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>

/**
 * @file
 * @brief Binary API messaging via shared memory
 * Low-level, primary provisioning interface
 */
/*? %%clicmd:group_label Binary API CLI %% ?*/
/*? %%syscfg:group_label Binary API configuration %% ?*/

#define TRACE_VLIB_MEMORY_QUEUE 0

#include <vlibmemory/vl_memory_msg_enum.h>	/* enumerate all vlib messages */

#define vl_typedefs		/* define message structures */
#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 <vlibmemory/vl_memory_api_h.h>
#undef vl_printfun

static inline void *
vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
				     void *handle)
{
  vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
	    a->plugin_name,
	    clib_host_to_net_u16 (a->first_msg_id),
	    clib_host_to_net_u16 (a->last_msg_id));
  return handle;
}

/* instantiate all the endian swap functions we know about */
#define vl_endianfun
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_endianfun

u8 *
vl_api_serialize_message_table (api_main_t * am, u8 * vector)
{
  serialize_main_t _sm, *sm = &_sm;
  hash_pair_t *hp;
  u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);

  serialize_open_vector (sm, vector);

  /* serialize the count */
  serialize_integer (sm, nmsg, sizeof (u32));

  /* *INDENT-OFF* */
  hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
  ({
    serialize_likely_small_unsigned_integer (sm, hp->value[0]);
    serialize_cstring (sm, (char *) hp->key);
  }));
  /* *INDENT-ON* */

  return serialize_close_vector (sm);
}

static void
vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
{
  vl_api_get_first_msg_id_reply_t *rmp;
  vl_api_registration_t *regp;
  uword *p;
  api_main_t *am = &api_main;
  vl_api_msg_range_t *rp;
  u8 name[64];
  u16 first_msg_id = ~0;
  int rv = -7;			/* VNET_API_ERROR_INVALID_VALUE */

  regp = vl_api_client_index_to_registration (mp->client_index);
  if (!regp)
    return;

  if (am->msg_range_by_name == 0)
    goto out;
  strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name));
  name[ARRAY_LEN (name) - 1] = '\0';
  p = hash_get_mem (am->msg_range_by_name, name);
  if (p == 0)
    goto out;

  rp = vec_elt_at_index (am->msg_ranges, p[0]);
  first_msg_id = rp->first_msg_id;
  rv = 0;

out:
  rmp = vl_msg_api_alloc (sizeof (*rmp));
  rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
  rmp->context = mp->context;
  rmp->retval = ntohl (rv);
  rmp->first_msg_id = ntohs (first_msg_id);
  vl_api_send_msg (regp, (u8 *) rmp);
}

void
vl_api_api_versions_t_handler (vl_api_api_versions_t * mp)
{
  api_main_t *am = &api_main;
  vl_api_api_versions_reply_t *rmp;
  vl_api_registration_t *reg;
  u32 nmsg = vec_len (am->api_version_list);
  int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
  int i;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  rmp = vl_msg_api_alloc (msg_size);
  memset (rmp, 0, msg_size);
  rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);

  /* fill in the message */
  rmp->context = mp->context;
  rmp->count = htonl (nmsg);

  for (i = 0; i < nmsg; ++i)
    {
      api_version_t *vl = &am->api_version_list[i];
      rmp->api_versions[i].major = htonl (vl->major);
      rmp->api_versions[i].minor = htonl (vl->minor);
      rmp->api_versions[i].patch = htonl (vl->patch);
      strncpy ((char *) rmp->api_versions[i].name, vl->name,
	       ARRAY_LEN (rmp->api_versions[i].name));
      rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] =
	'\0';
    }

  vl_api_send_msg (reg, (u8 *) rmp);
}

#define foreach_vlib_api_msg                            \
_(GET_FIRST_MSG_ID, get_first_msg_id)                   \
_(API_VERSIONS, api_versions)

/*
 * vl_api_init
 */
static int
vlib_api_init (void)
{
  vl_msg_api_msg_config_t cfg;
  vl_msg_api_msg_config_t *c = &cfg;

  memset (c, 0, sizeof (*c));

#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 _

  return 0;
}

u64 vector_rate_histogram[SLEEP_N_BUCKETS];

/*
 * Callback to send ourselves a plugin numbering-space trace msg
 */
static void
send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
{
  vl_api_trace_plugin_msg_ids_t *mp;
  api_main_t *am = &api_main;
  vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
  svm_queue_t *q;

  mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));

  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
  strncpy ((char *) mp->plugin_name, (char *) name,
	   sizeof (mp->plugin_name) - 1);
  mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
  mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);

  q = shmem_hdr->vl_input_queue;

  vl_msg_api_send_shmem (q, (u8 *) & mp);
}

void
vl_api_save_msg_table (void)
{
  u8 *serialized_message_table;
  api_main_t *am = &api_main;
  u8 *chroot_file;
  int fd, rv;

  /*
   * Snapshoot the api message table.
   */
  if (strstr ((char *) am->save_msg_table_filename, "..")
      || index ((char *) am->save_msg_table_filename, '/'))
    {
      clib_warning ("illegal save-message-table filename '%s'",
		    am->save_msg_table_filename);
      return;
    }

  chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);

  fd = creat ((char *) chroot_file, 0644);

  if (fd < 0)
    {
      clib_unix_warning ("creat");
      return;
    }

  serialized_message_table = vl_api_serialize_message_table (am, 0);

  rv = write (fd, serialized_message_table,
	      vec_len (serialized_message_table));

  if (rv != vec_len (serialized_message_table))
    clib_unix_warning ("write");

  rv = close (fd);
  if (rv < 0)
    clib_unix_warning ("close");

  vec_free (chroot_file);
  vec_free (serialized_message_table);
}

static uword
vl_api_clnt_process (vlib_main_t * vm, vlib_node_runtime_t * node,
		     vlib_frame_t * f)
{
  int private_segment_rotor = 0, i, rv;
  vl_socket_args_for_process_t *a;
  vl_shmem_hdr_t *shm;
  svm_queue_t *q;
  clib_error_t *e;
  api_main_t *am = &api_main;
  f64 dead_client_scan_time;
  f64 sleep_time, start_time;
  f64 vector_rate;
  clib_error_t *error;
  uword event_type;
  uword *event_data = 0;
  f64 now;

  if ((error = vl_sock_api_init (vm)))
    {
      clib_error_report (error);
      clib_warning ("socksvr_api_init failed, quitting...");
      return 0;
    }

  if ((rv = vlib_api_init ()) < 0)
    {
      clib_warning ("vlib_api_init returned %d, quitting...", rv);
      return 0;
    }

  shm = am->shmem_hdr;
  q = shm->vl_input_queue;

  e = vlib_call_init_exit_functions
    (vm, vm->api_init_function_registrations, 1 /* call_once */ );
  if (e)
    clib_error_report (e);

  sleep_time = 10.0;
  dead_client_scan_time = vlib_time_now (vm) + 10.0;

  /*
   * Send plugin message range messages for each plugin we loaded
   */
  for (i = 0; i < vec_len (am->msg_ranges); i++)
    {
      vl_api_msg_range_t *rp = am->msg_ranges + i;
      send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
				   rp->last_msg_id);
    }

  /*
   * Save the api message table snapshot, if configured
   */
  if (am->save_msg_table_filename)
    vl_api_save_msg_table ();

  /* $$$ pay attention to frame size, control CPU usage */
  while (1)
    {
      /*
       * There's a reason for checking the queue before
       * sleeping. If the vlib application crashes, it's entirely
       * possible for a client to enqueue a connect request
       * during the process restart interval.
       *
       * Unless some force of physics causes the new incarnation
       * of the application to process the request, the client will
       * sit and wait for Godot...
       */
      vector_rate = vlib_last_vector_length_per_node (vm);
      start_time = vlib_time_now (vm);
      while (1)
	{
	  if (vl_mem_api_handle_msg_main (vm, node))
	    {
	      vm->api_queue_nonempty = 0;
	      VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0);
	      sleep_time = 20.0;
	      break;
	    }

	  /* Allow no more than 10us without a pause */
	  if (vlib_time_now (vm) > start_time + 10e-6)
	    {
	      int index = SLEEP_400_US;
	      if (vector_rate > 40.0)
		sleep_time = 400e-6;
	      else if (vector_rate > 20.0)
		{
		  index = SLEEP_200_US;
		  sleep_time = 200e-6;
		}
	      else if (vector_rate >= 1.0)
		{
		  index = SLEEP_100_US;
		  sleep_time = 100e-6;
		}
	      else
		{
		  index = SLEEP_10_US;
		  sleep_time = 10e-6;
		}
	      vector_rate_histogram[index] += 1;
	      break;
	    }
	}

      /*
       * 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)))
	{
	  if (private_segment_rotor >= vec_len (am->vlib_private_rps))
	    private_segment_rotor = 0;
	  vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++);
	}

      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;
	  VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize);

	  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_socket_process_api_msg (a->clib_file, a->regp,
					 (i8 *) a->data);
	      vec_free (a->data);
	      pool_put (socket_main.process_args, a);
	    }
	  break;

	  /* Timeout... */
	case -1:
	  break;

	default:
	  clib_warning ("unknown event type %d", event_type);
	  break;
	}

      if (now > dead_client_scan_time)
	{
	  vl_mem_api_dead_client_scan (am, shm, now);
	  dead_client_scan_time = vlib_time_now (vm) + 10.0;
	}
    }

  return 0;
}
/* *INDENT-OFF* */
VLIB_REGISTER_NODE (vl_api_clnt_node) =
{
  .function = vl_api_clnt_process,
  .type = VLIB_NODE_TYPE_PROCESS,
  .name = "api-rx-from-ring",
  .state = VLIB_NODE_STATE_DISABLED,
};
/* *INDENT-ON* */

void
vl_mem_api_enable_disable (vlib_main_t * vm, int enable)
{
  vlib_node_set_state (vm, vl_api_clnt_node.index,
		       (enable
			? VLIB_NODE_STATE_POLLING
			: VLIB_NODE_STATE_DISABLED));
}

static uword
api_rx_from_node (vlib_main_t * vm,
		  vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  uword n_packets = frame->n_vectors;
  uword n_left_from;
  u32 *from;
  static u8 *long_msg;

  vec_validate (long_msg, 4095);
  n_left_from = frame->n_vectors;
  from = vlib_frame_args (frame);

  while (n_left_from > 0)
    {
      u32 bi0;
      vlib_buffer_t *b0;
      void *msg;
      uword msg_len;

      bi0 = from[0];
      b0 = vlib_get_buffer (vm, bi0);
      from += 1;
      n_left_from -= 1;

      msg = b0->data + b0->current_data;
      msg_len = b0->current_length;
      if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
	{
	  ASSERT (long_msg != 0);
	  _vec_len (long_msg) = 0;
	  vec_add (long_msg, msg, msg_len);
	  while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
	    {
	      b0 = vlib_get_buffer (vm, b0->next_buffer);
	      msg = b0->data + b0->current_data;
	      msg_len = b0->current_length;
	      vec_add (long_msg, msg, msg_len);
	    }
	  msg = long_msg;
	}
      vl_msg_api_handler_no_trace_no_free (msg);
    }

  /* Free what we've been given. */
  vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);

  return n_packets;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
    .function = api_rx_from_node,
    .type = VLIB_NODE_TYPE_INTERNAL,
    .vector_size = 4,
    .name = "api-rx-from-node",
};
/* *INDENT-ON* */

static void
vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
{
  vl_api_rpc_call_reply_t *rmp;
  int (*fp) (void *);
  i32 rv = 0;
  vlib_main_t *vm = vlib_get_main ();

  if (mp->function == 0)
    {
      rv = -1;
      clib_warning ("rpc NULL function pointer");
    }

  else
    {
      if (mp->need_barrier_sync)
	vlib_worker_thread_barrier_sync (vm);

      fp = uword_to_pointer (mp->function, int (*)(void *));
      rv = fp (mp->data);

      if (mp->need_barrier_sync)
	vlib_worker_thread_barrier_release (vm);
    }

  if (mp->send_reply)
    {
      svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
      if (q)
	{
	  rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
	  rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
	  rmp->context = mp->context;
	  rmp->retval = rv;
	  vl_msg_api_send_shmem (q, (u8 *) & rmp);
	}
    }
  if (mp->multicast)
    {
      clib_warning ("multicast not yet implemented...");
    }
}

static void
vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
{
  clib_warning ("unimplemented");
}

void
vl_api_send_pending_rpc_requests (vlib_main_t * vm)
{
  api_main_t *am = &api_main;
  vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
  svm_queue_t *q;
  int i;

  /*
   * Use the "normal" control-plane mechanism for the main thread.
   * Well, almost. if the main input queue is full, we cannot
   * block. Otherwise, we can expect a barrier sync timeout.
   */
  q = shmem_hdr->vl_input_queue;

  for (i = 0; i < vec_len (vm->pending_rpc_requests); i++)
    {
      while (pthread_mutex_trylock (&q->mutex))
	vlib_worker_thread_barrier_check ();

      while (PREDICT_FALSE (svm_queue_is_full (q)))
	{
	  pthread_mutex_unlock (&q->mutex);
	  vlib_worker_thread_barrier_check ();
	  while (pthread_mutex_trylock (&q->mutex))
	    vlib_worker_thread_barrier_check ();
	}

      vl_msg_api_send_shmem_nolock (q, (u8 *) (vm->pending_rpc_requests + i));

      pthread_mutex_unlock (&q->mutex);
    }
  _vec_len (vm->pending_rpc_requests) = 0;
}

always_inline void
vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
				    u8 force_rpc)
{
  vl_api_rpc_call_t *mp;
  vlib_main_t *vm = vlib_get_main ();

  /* Main thread and not a forced RPC: call the function directly */
  if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
    {
      void (*call_fp) (void *);

      vlib_worker_thread_barrier_sync (vm);

      call_fp = fp;
      call_fp (data);

      vlib_worker_thread_barrier_release (vm);
      return;
    }

  /* Otherwise, actually do an RPC */
  mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);

  memset (mp, 0, sizeof (*mp));
  clib_memcpy (mp->data, data, data_length);
  mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
  mp->function = pointer_to_uword (fp);
  mp->need_barrier_sync = 1;

  vec_add1 (vm->pending_rpc_requests, (uword) mp);
}

/*
 * Check if called from worker threads.
 * If so, make rpc call of fp through shmem.
 * Otherwise, call fp directly
 */
void
vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
{
  vl_api_rpc_call_main_thread_inline (fp, data, data_length,	/*force_rpc */
				      0);
}

/*
 * Always make rpc call of fp through shmem, useful for calling from threads
 * not setup as worker threads, such as DPDK callback thread
 */
void
vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
{
  vl_api_rpc_call_main_thread_inline (fp, data, data_length,	/*force_rpc */
				      1);
}

static void
vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
{
  api_main_t *am = &api_main;
  vl_api_msg_range_t *rp;
  uword *p;

  /* Noop (except for tracing) during normal operation */
  if (am->replay_in_progress == 0)
    return;

  p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
  if (p == 0)
    {
      clib_warning ("WARNING: traced plugin '%s' not in current image",
		    mp->plugin_name);
      return;
    }

  rp = vec_elt_at_index (am->msg_ranges, p[0]);
  if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
    {
      clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
		    mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
		    rp->first_msg_id);
    }

  if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
    {
      clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
		    mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
		    rp->last_msg_id);
    }
}

#define foreach_rpc_api_msg                     \
_(RPC_CALL,rpc_call)                            \
_(RPC_CALL_REPLY,rpc_call_reply)

#define foreach_plugin_trace_msg		\
_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)

/*
 * Set the rpc callback at our earliest possible convenience.
 * This avoids ordering issues between thread_init() -> start_workers and
 * an init function which we could define here. If we ever intend to use
 * vlib all by itself, we can't create a link-time dependency on
 * an init function here and a typical "call foo_init first"
 * guitar lick.
 */

extern void *rpc_call_main_thread_cb_fn;

static clib_error_t *
rpc_api_hookup (vlib_main_t * vm)
{
  api_main_t *am = &api_main;
#define _(N,n)                                                  \
    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
                           vl_api_##n##_t_handler,              \
                           vl_noop_handler,                     \
                           vl_noop_handler,			\
                           vl_api_##n##_t_print,                \
                           sizeof(vl_api_##n##_t), 0 /* do not trace */);
  foreach_rpc_api_msg;
#undef _

#define _(N,n)                                                  \
    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
                           vl_api_##n##_t_handler,              \
                           vl_noop_handler,                     \
                           vl_noop_handler,			\
                           vl_api_##n##_t_print,                \
                           sizeof(vl_api_##n##_t), 1 /* do trace */);
  foreach_plugin_trace_msg;
#undef _

  /* No reason to halt the parade to create a trace record... */
  am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
  rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
  return 0;
}

VLIB_API_INIT_FUNCTION (rpc_api_hookup);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */