Dear guys and gals
I have composed a plugin called cgnat and I was trying hard to call the plugin 
from a C program to make a portable interface.
I would be very thankful if anyone could help me by providing a sample program 
which could communicate with a vpp plugin or finds my fault in my program.
I got no compilation or linking error.
All values like max_user or retval are defined in plugin.
The plugin has a .so file in /usr/lib/vpp_plugins.
Here I attached my main.c file and my Makefile.
Any help would be appreciated because I got one and a half month of my life 
trying different methods.
By the way I have worked with python API and it works just fine but I want to 
call the plugin from C API.
Thank you very much
Seyyed Mojtaba Rezvani

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.

View/Reply Online (#9574): https://lists.fd.io/g/vpp-dev/message/9574
Mute This Topic: https://lists.fd.io/mt/21929043/21656
Mute #vpp: https://lists.fd.io/mk?hashtag=vpp&subid=1480452
Group Owner: vpp-dev+ow...@lists.fd.io
Leave: https://lists.fd.io/g/vpp-dev/unsub  [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

/*
  I have build a plugin called cgnat and I wanted to set some value to it and get some from it
  in get function it responds but incorrectly
  in set function t wont responds
  No compilation erro or warning I got
  All values are defined
  The thing I dont know is just how to connect to a vpp plugin
  Thanks
*/

#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>
#include <vppinfra/error.h>
//#include<vlibmemory/memclnt.api.h>//s
#include <vnet/vnet.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>

#include <api/cgnat_msg_enum.h>

#include <vnet/ip/ip.h>
#include <vnet/interface.h>

//#include <api/cgnat_api.h>

#include <cgnat.h>

#define f64_endian(a)
#define f64_print(a,b)

#define vl_typedefs             /* define message structures */
#include <api/cgnat_all_api_h.h>
#undef vl_typedefs

#define vl_endianfun            /* define message structures */
#include <api/cgnat_all_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...)
#define vl_printfun
#include <api/cgnat_all_api_h.h>
#undef vl_printfun

#define vl_api_version(n,v) static u32 api_version=(v);
#include <api/cgnat.api.h>
#undef vl_api_version

vl_shmem_hdr_t *shmem_hdr;

typedef struct
{
  int link_events_on;
  int stats_on;
  int oam_events_on;

  /* convenience */
  u16 msg_id_base;
  unix_shared_memory_queue_t *vl_input_queue;
  u32 my_client_index;
} test_main_t;

test_main_t test_main;
vlib_main_t vlib_global_main;
vlib_main_t **vlib_mains;


void
vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
{
  clib_warning ("vlib_cli_output callled...");
}

static void vl_api_get_nat44_max_user_details_t_handler
  (vl_api_get_nat44_max_user_details_t * mp)
{
  fformat (stdout, "Maximum number of users is %d\n", ntohl(mp->context));
}

static void vl_api_config_nat44_max_user_reply_t_handler
  (vl_api_config_nat44_max_user_reply_t * mp)
{
  fformat (stdout, "Return value is %d\n", ntohl (mp->retval));
}

static void noop_handler(void *notused)
{}

#define vl_api_vnet_ip4_fib_counters_t_endian noop_handler
#define vl_api_vent_ip4_fib_counters_t_print noop_handler
#define vl_api_vnet_ip6_fib_counters_t_endian noop_handler
#define vl_api_vnet_ip6_fib_counters_t_print noop_handler

#define foreach_api_msg             		                                \
_(CONFIG_NAT44_MAX_USER_REPLY, config_nat44_max_user_reply)			\
_(GET_NAT44_MAX_USER_DETAILS, get_nat44_max_user_details)

int
connect_to_vpe (char *name)
{

  int rv = 0;

  rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);

#define _(N,n)                                                  \
    vl_msg_api_set_handlers(VL_API_##N  , #n,                     \
                           vl_api_##n##_t_handler,              \
                           noop_handler,                        \
                           vl_api_##n##_t_endian,               \
                           vl_api_##n##_t_print,                \
                           sizeof(vl_api_##n##_t), 1);
  foreach_api_msg;
#undef _

  shmem_hdr = api_main.shmem_hdr;

  return rv;
}

int
disconnect_from_vpe (void)
{
  vl_client_disconnect_from_vlib ();
  clib_warning("not here");
  return 0;
}

void
get_max_users (test_main_t * tm)
{
  vl_api_get_nat44_max_user_dump_t *mp;
  mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));
  mp->_vl_msg_id = ntohs (VL_API_GET_NAT44_MAX_USER_DUMP);
  mp->client_index = tm->my_client_index;
  mp->context = 0xdeadbeef;
  vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
}

void
set_max_users (test_main_t * tm)
{
  vl_api_config_nat44_max_user_t *mp;
  mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));
  mp->_vl_msg_id = ntohs (VL_API_CONFIG_NAT44_MAX_USER);
  mp->client_index = tm->my_client_index;
  mp->context = 0xdeadbeef;
  mp->max_user = 0;
  mp->is_unset = 0;
  vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
  clib_warning("set_max_user is called");
}

int
main (int argc, char **argv)
{
  api_main_t *am = &api_main;
  test_main_t *tm = &test_main;
  int ch;
  u32 rv = -1;
  rv = connect_to_vpe ("test");
  clib_warning("rv is %d", rv);
  tm->vl_input_queue = shmem_hdr->vl_input_queue;
  tm->my_client_index = am->my_client_index;
  fformat (stdout, "Type 'h' for help, 'q' to quit...\n");
  while (1)
    {
      ch = getchar ();
      switch (ch)
        {
        case 'q':
          fformat (stdout, "THE END\n");
          return 0;
        case 's':
          set_max_users(tm);
          break;
        case 'g':
          get_max_users(tm);
          break;
        case 'h':
          fformat (stdout, "q: Quit\n");
          fformat (stdout, "h: Help\n");
          fformat (stdout, "s: Set maximum users\n");
          fformat (stdout, "g: Get maximum users\n");

        default:
          break;
        }
    }
  disconnect_from_vpe ();
  exit (0);
}

//void
//vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
//{
  /*
   * Send the main API signature in slot 0. This bit of code must
   * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
   */
  //mp->api_versions[0] = clib_host_to_net_u32 (cgnat_api_version);
//}

Attachment: Makefile
Description: Binary data

Reply via email to