前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ubuntu系统运行VPP24.02系列:main函数初始化介绍

Ubuntu系统运行VPP24.02系列:main函数初始化介绍

作者头像
通信行业搬砖工
发布2024-06-13 20:37:25
640
发布2024-06-13 20:37:25
举报
文章被收录于专栏:网络虚拟化网络虚拟化

01、思科VPP软件架构介绍

官方描述

如下图所示:官方文档介绍了VPP软件架构。

从官方的文档描述上我们可知:

1、VPP Infra ( VPP infrastructure layer 基础结构层)

提供一些基本的通用的功能函数库:包括内存管理,向量操作,hash, timer、pool、bimap等

2、Vlib (vector processing library)

主要提供基本的应用程序库:buffer管理,graph node管理,线程,CLI,trace等

3、Vnet (vpp network stack)

提供网络资源能力:比如设备,L2,L3,L4功能,session管理,控制管理,流量管理等。

4、 Plugins 以插件形式实现相关协议功能

主要为实现一些功能,在程序启动的时候加载,一般情况下会在插件中加入一些node节点去实现相关功能,比如nat、pppoe、dhcp等。

源码架构展示

代码语言:javascript
复制
root@inspur6680:/home/ubuntu/work/vpp# git  branch
* stable/2402
root@inspur6680:/home/ubuntu/work/vpp# ls -ls
total 112
 4 drwxr-xr-x  3 root root  4096  5月 23 14:15 build
 4 drwxr-xr-x  4 root root  4096  5月 23 14:15 build-data
 4 drwxr-xr-x  6 root root  4096  5月 23 14:51 build-root
 4 -rwxr-xr-x  1 root root  4000  5月 23 14:15 configure
 4 drwxr-xr-x 14 root root  4096  5月 23 14:15 docs
 4 drwxr-xr-x 28 root root  4096  5月 23 14:15 extras
 4 -rw-r--r--  1 root root  2876  5月 23 14:15 INFO.yaml
12 -rw-r--r--  1 root root 11357  5月 23 14:15 LICENSE
20 -rw-r--r--  1 root root 18109  5月 23 14:15 MAINTAINERS
28 -rw-r--r--  1 root root 25020  5月 23 14:15 Makefile
 8 -rw-r--r--  1 root root  4340  5月 23 14:15 README.md
 4 drwxr-xr-x 19 root root  4096  5月 23 14:15 src
12 drwxr-xr-x  6 root root 12288  5月 23 14:15 test
root@inspur6680:/home/ubuntu/work/vpp#

软件架构图

如下图所示,展示vpp软件架构设计和相关模块的功能描述。

02、VPP启动介绍

思科VPP(Vector Packet Processing)软件架构在实现具体业务功能的时,是通过插件的形式进行。

我们以acl 插件为例:

代码语言:javascript
复制
#CMakelists
add_vpp_plugin(acl
  SOURCES
  acl.c
  hash_lookup.c
  lookup_context.c
  sess_mgmt_node.c
  dataplane_node.c
  dataplane_node_nonip.c

  MULTIARCH_SOURCES
  dataplane_node.c
  dataplane_node_nonip.c

  API_FILES
  acl.api
  acl_types.api

  API_TEST_SOURCES
  acl_test.c
)

在该文件里面介绍了编译ACL插件所依赖的代码文件,以及相关功能需要的api文件,该部分最后编译完成是以动态库的形式存在。至于插件的禁止和加载,可以参考上节内容描述。Ubuntu系统运行VPP24.02系列:startup.conf配置文件解读

在我们实现acl功能的时候,其实我们就是添加了acl这个模块的相关node,当数据经过前面的一些节点处理的时候,按照业务node节点的编排,将对应的业务流量送入该节点处理。(vpp的节点编排、feature机制后续文章会介绍)

02、VPP初始化介绍

思科VPP(Vector Packet Processing)软件架构在实现具体的业务功能的时,是通过插件的形式进行。

代码路径src/vpp/vnet/main.c

代码语言:javascript
复制
int
main (int argc, char *argv[])
{
  int i;
  void vl_msg_api_set_first_available_msg_id (u16);
  uword main_heap_size = (1ULL << 30);
  u8 *sizep;
  u32 size;
  clib_mem_page_sz_t main_heap_log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
  clib_mem_page_sz_t default_log2_hugepage_sz = CLIB_MEM_PAGE_SZ_UNKNOWN;
  unformat_input_t input, sub_input;
  u8 *s = 0, *v = 0;
  int main_core = 1;
  cpu_set_t cpuset;
  void *main_heap;

#if __x86_64__
  CLIB_UNUSED (const char *msg)
    = "ERROR: This binary requires CPU with %s extensions.\n";
#define _(a,b)                                  \
    if (!clib_cpu_supports_ ## a ())            \
      {                                         \
  fprintf(stderr, msg, b);                \
  exit(1);                                \
      }

#if __AVX2__
  _(avx2, "AVX2")
#endif
#if __AVX__
    _(avx, "AVX")
#endif
#if __SSE4_2__
    _(sse42, "SSE4.2")
#endif
#if __SSE4_1__
    _(sse41, "SSE4.1")
#endif
#if __SSSE3__
    _(ssse3, "SSSE3")
#endif
#if __SSE3__
    _(sse3, "SSE3")
#endif
#undef _
#endif
    /*
     * Load startup config from file.
     * usage: vpp -c /etc/vpp/startup.conf
     */
    if ((argc == 3) && !strncmp (argv[1], "-c", 2))
    {
      FILE *fp;
      char inbuf[4096];
      int argc_ = 1;
      char **argv_ = NULL;
      char *arg = NULL;
      char *p;

      fp = fopen (argv[2], "r");
      if (fp == NULL)
  {
    fprintf (stderr, "open configuration file '%s' failed\n", argv[2]);
    return 1;
  }
      argv_ = calloc (1, sizeof (char *));
      if (argv_ == NULL)
  {
    fclose (fp);
    return 1;
  }
      arg = strndup (argv[0], 1024);
      if (arg == NULL)
  {
    fclose (fp);
    free (argv_);
    return 1;
  }
      argv_[0] = arg;

      while (1)
  {
    if (fgets (inbuf, 4096, fp) == 0)
      break;
    p = strtok (inbuf, " \t\n");
    while (p != NULL)
      {
        if (*p == '#')
    break;
        argc_++;
        char **tmp = realloc (argv_, argc_ * sizeof (char *));
        if (tmp == NULL)
    return 1;
        argv_ = tmp;
        arg = strndup (p, 1024);
        if (arg == NULL)
    return 1;
        argv_[argc_ - 1] = arg;
        p = strtok (NULL, " \t\n");
      }
  }

      fclose (fp);

      char **tmp = realloc (argv_, (argc_ + 1) * sizeof (char *));
      if (tmp == NULL)
  return 1;
      argv_ = tmp;
      argv_[argc_] = NULL;

      argc = argc_;
      argv = argv_;
    }

  /*
   * Look for and parse the "heapsize" config parameter.
   * Manual since none of the clib infra has been bootstrapped yet.
   *
   * Format: heapsize <nn>[mM][gG]
   */

  for (i = 1; i < (argc - 1); i++)
    {
      if (!strncmp (argv[i], "plugin_path", 11))
  {
    if (i < (argc - 1))
      vlib_plugin_path = argv[++i];
  }
      if (!strncmp (argv[i], "test_plugin_path", 16))
  {
    if (i < (argc - 1))
      vat_plugin_path = argv[++i];
  }
      else if (!strncmp (argv[i], "heapsize", 8))
  {
    sizep = (u8 *) argv[i + 1];
    size = 0;
    while (*sizep >= '0' && *sizep <= '9')
      {
        size *= 10;
        size += *sizep++ - '0';
      }
    if (size == 0)
      {
        fprintf
    (stderr,
     "warning: heapsize parse error '%s', use default %lld\n",
     argv[i], (long long int) main_heap_size);
        goto defaulted;
      }

    main_heap_size = size;

    if (*sizep == 'g' || *sizep == 'G')
      main_heap_size <<= 30;
    else if (*sizep == 'm' || *sizep == 'M')
      main_heap_size <<= 20;
  }
      else if (!strncmp (argv[i], "main-core", 9))
  {
    if (i < (argc - 1))
      {
        errno = 0;
        unsigned long x = strtol (argv[++i], 0, 0);
        if (errno == 0)
    main_core = x;
      }
  }
    }
defaulted:

  /* temporary heap */
  clib_mem_init (0, 1 << 20);
  unformat_init_command_line (&input, (char **) argv);

  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (&input, "memory %v", &v))
  {
    unformat_init_vector (&sub_input, v);
    v = 0;
    while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
      {
        if (unformat (&sub_input, "main-heap-size %U",
          unformat_memory_size, &main_heap_size))
    ;
        else if (unformat (&sub_input, "main-heap-page-size %U",
         unformat_log2_page_size,
         &main_heap_log2_page_sz))
    ;
        else if (unformat (&sub_input, "default-hugepage-size %U",
         unformat_log2_page_size,
         &default_log2_hugepage_sz))
    ;
        else
    {
      fformat (stderr, "unknown 'memory' config input '%U'\n",
         format_unformat_error, &sub_input);
      exit (1);
    }

      }
    unformat_free (&sub_input);
  }
      else if (!unformat (&input, "%s %v", &s, &v))
  break;

      vec_reset_length (s);
      vec_reset_length (v);
    }
  vec_free (s);
  vec_free (v);

  unformat_free (&input);

  /* set process affinity for main thread */
  CPU_ZERO (&cpuset);
  CPU_SET (main_core, &cpuset);
  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);

  /* Set up the plugin message ID allocator right now... */
  vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);

  /* destroy temporary heap and create main one */
  clib_mem_destroy ();

  if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
             main_heap_log2_page_sz)))
    {
      /* Figure out which numa runs the main thread */
      __os_numa_index = clib_get_current_numa_node ();

      if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
  clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);

      /* and use the main heap as that numa's numa heap */
      clib_mem_set_per_numa_heap (main_heap);
      vlib_main_init ();
      vpe_main_init (vlib_get_first_main ());
      return vlib_unix_main (argc, argv);
    }
  else
    {
      {
  int rv __attribute__ ((unused)) =
    write (2, "Main heap allocation failure!\r\n", 31);
      }
      return 1;
    }
}

在当前函数的处理流程中,主要进行如下操作

1、判断CPU型号是否支持情况;

2、将运行命令 vpp -c PATH/startup.conf文件进行解析,获取相关配置

3、读取空间配置参数,并且进行相关解析配置

4、配置CPU亲和性,将当前运行的线程同main线程进行绑定。相关函数pthread_setaffinity_np

代码语言:javascript
复制
  CPU_ZERO (&cpuset);
  CPU_SET (main_core, &cpuset);
  pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset);

5、通过vpp消息机制发送消息

代码语言:javascript
复制
  vl_msg_api_set_first_available_msg_id (VL_MSG_MEMCLNT_LAST + 1);

6、然后在这个过程中,对numa,heap等空间相关的操作代码中有相关注释,通过调用 vlib_unix_main (argc, argv);进行进一步的初始化。vlib_unix_main (argc, argv)这个函数它初始化全局状态、配置、插件以及其他运行时环境,并最终进入主循环以运行网络处理和其他功能。

代码语言:javascript
复制

  if ((main_heap = clib_mem_init_with_page_size (main_heap_size,
             main_heap_log2_page_sz)))
    {
      /* Figure out which numa runs the main thread */
      __os_numa_index = clib_get_current_numa_node ();

      if (default_log2_hugepage_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
  clib_mem_set_log2_default_hugepage_size (default_log2_hugepage_sz);

      /* and use the main heap as that numa's numa heap */
      clib_mem_set_per_numa_heap (main_heap);
      vlib_main_init ();
      vpe_main_init (vlib_get_first_main ());
      return vlib_unix_main (argc, argv);
    }

关于vlib_unix_main (argc, argv); 函数的初始化,我们下期继续讨论,谢谢!

作者简介

作者:通信行业搬砖工 云网络高级软件工程师

数通领域行业从业人员

现云网络行业从业者

(正文完)

END

转载与投稿

文章转载需注明:来源公众号:通信行业搬砖工,并且附上链接

文章错误之处,欢迎指导斧正,各位大拿留言交流,探讨技术。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 通信行业搬砖工 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档