// cc-oci-runtime/src/oci.c

/*!

* Create the state file, apply mounts and run hooks, but do not start the VM

*/

gboolean cc_oci_create(struct cc_oci_config *config)

(1)、依次调用cc_oci_config_file_parse(config),cc_oci_config_check(config),cc_oci_runtime_dir_setup(config)和cc_oci_handle_mounts(config)

(2)、当cc_pod_is_vm(config)为true时,调用cc_oci_vm_launch(config),否则调用cc_pod_container_create(config)

// cc-oci-runtime/src/process.c

/*!

* Start the hypervisor as a child process.

* Due to the way networking is handled in Docker, the logic here is unfortunately rather complex.

* \param config \ref cc_oci_config.

* \return \c true on success, else \c false.

*/

gboolean cc_oci_vm_launch(struct cc_oci_config *config)

(1)、调用setup_networking = cc_oci_enable_networking();   --->仅仅只是确定是否是root,因为要创建interface

...

(2)、调用config->state.status = OCI_STATUS_CREATED;

/* The namespace setup occurs in the parent to ensure the hooks run successfully. The child will

* automatically inherit the namespaces.

*/

(3)、调用cc_oci_ns_setup(config)

/* Connect to the proxy before launching the shim so that the proxy socket fd can be passed to the shim*/

(4)、调用cc_proxy_connect(config->proxy)

/* Set up comms channels to the child:

* - one to pass the full list of expanded hypervisor arguments.

* - one to allow detection of successful child setup: if the child closes the pipe,

* it was successful, but if it writes data to the pipe, setup failed.

*/

(5)、调用pipe2(child_err_pipe, O_CLOEXEC)和pipe2(hypervisor_args_pipe, O_CLOEXEC)

(6)、fork()一个子进程,在子进程中最终运行cc_oci_setup_child(config)以及execvp(args[0], args)

....

/* Run the pre-start hooks.

* Note that one of these hooks will configure the networking in the network namespace.

* If a hook returns a non-zero exit code, then an error including the exit code and the stderr is

* returned to the caller and the container is torn down.

*/

(7)、hook_status = cc_run_hooks(config->oci.hooks.prestart, config->state.state_file_path, true)

// add network config bits to following functions:

// - cc_oci_container_state()

// - oci_state()

// - cc_oci_update_options()

(8)、如果setup_networking为true,则依次调用hndl = netlink_init(),cc_oci_vm_netcfg_get(config, hndl)和cc_oci_network_create(config, hndl)

.......

// cc-oci-runtime/src/netlink.c

/*!

* Setup the netlink socket to use with netlink transactions.This handle should be used for all netlink

* transactions for a given thread.

*/

struct netlink_handle *netlink_init(void)

初始化一个netlink_handle实例

// cc-oci-runtime/src/process.c

/*!

* Obtain the network configuration by quering the network namespace.

* \param[in, out] config \ref cc_oci_config.

* \param hndl handle returned from a call to \ref netlink_init().

*/

private gboolean cc_oci_vm_netcfg_get(struct cc_oci_config *config, struct netlink_handle *hndl)

仅仅调用cc_oci_network_discover(config, hndl)

// cc-oci-runtime/src/networking.c

/*!

* Obtain the networking configuration of the container

* Currently done by scanned the namespace

* Ideally the OCI spec should be modified such that

* these parameters are sent to the runtime

*/

gboolean cc_oci_network_discover(struct cc_oci_config *const config, struct netlink_handle *hndl)

...

(1)、调用getifaddrs(&ifaddrs)  --> discover container interfaces

....

/*!

* Request to create the networking framework that will be used to

* connect the specified container network(veth) to the VM

*

* The container may be associated with multiple networks and function has to be invoked

* for each of those networks

* Once the OCI spec supports the creation of VM compatible tap interfaces in the network plugin

* this setup will not be required

*/

gboolean cc_oci_network_create(const struct cc_oci_config *const config, struct netlink_handle *const hndl)

/* Each container has its own namespace. Hence we use the same mac address prefix

* for tap interfaces on the host side. This method scales to support upto 2^16 networks

*/

遍历config->net.interfaces,

// cc-oci-runtime/src/namespace.c

/**

* Setup namespace.

* This should not strictly be required (since the runtime does not implement a "traditional linux" container).

* Howerver, namespace are used to pass network configuration to the runtime so the network namespace

* must be supported.

* \param config \ref cc_oci_config.

* \return \c true on success, else \c false.

* \todo Show the namespace path. For unshare, the strategy should be to call cc_oci_resolve_path (),

* passing it the value of ."/proc/self/ns/%s". The complication is that %s does *NOT* match the

* namespace names chosen by OCI, hence oci_ns_map will need to be extended to add a "gchar *proc_name" element

* \note in the case of error, check the value of errno immediately after this call to determine the reason.

*/

gooblean cc_oci_ns_setup(struct cc_oci_config *config)

从config中解析出network space的ns->path,并调用fd = open(ns->path, O_RDONLY),最后调用setns(fd, ns->type)加入该network namespace

ClearContainer 网络部分源码分析的更多相关文章

  1. docker网络部分源码分析

    daemon初始化network controller daemon的配置,网络部分的内容在cmd/dockerd/config_common_unix.go中指定,默认设置一般都为空 // daem ...

  2. Docker源码分析(八):Docker Container网络(下)

    1.Docker Client配置容器网络模式 Docker目前支持4种网络模式,分别是bridge.host.container.none,Docker开发者可以根据自己的需求来确定最适合自己应用场 ...

  3. ABP源码分析一:整体项目结构及目录

    ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...

  4. HashMap与TreeMap源码分析

    1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...

  5. nginx源码分析之网络初始化

    nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...

  6. zookeeper源码分析之五服务端(集群leader)处理请求流程

    leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...

  7. zookeeper源码分析之四服务端(单机)处理请求流程

    上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...

  8. zookeeper源码分析之三客户端发送请求流程

    znode 可以被监控,包括这个目录节点中存储的数据的修改,子节点目录的变化等,一旦变化可以通知设置监控的客户端,这个功能是zookeeper对于应用最重要的特性,通过这个特性可以实现的功能包括配置的 ...

  9. java使用websocket,并且获取HttpSession,源码分析

    转载请在页首注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6238826.html 一:本文使用范围 此文不仅仅局限于spring boot,普通的sprin ...

随机推荐

  1. ASP.Net Core MVC6 RC2 启动过程分析[偏源码分析]

    入口程序 如果做过Web之外开发的人,应该记得这个是标准的Console或者Winform的入口.为什么会这样呢? .NET Web Development and Tools Blog ASP.NE ...

  2. 用C#读取相片(JPG图片)的EXIF信息的方法

    引言:EXIF,是英文Exchangeable Image File{}#endregion#region 数据转换结构/// summary>/// 转换数据结构/// /summary> ...

  3. 让服务器iis支持.apk文件下载的设置方法

    随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站客户端,.apk文件就是安卓(Android)的应用程序后缀名,默认情况下,使用IIS作为Web服务器的无法下载此文件 ...

  4. 自从升级到macOS后,整个人都不好了

    电脑一直莫名的随机卡死,各种软件都出现了一些崩溃和不稳定的情况. Siri就是个笑话,启用后就开始索引本地硬盘,不管你有没有正在工作:直到你启动Siri,会暂停一下,然后就算是你开在哪不动,过两分钟, ...

  5. Java关于Properties用法(二)——替换配置文件中的参数

    上一章讲了配置文件的基本用法,虽然上一章已经可以解决一些需求,但还不些不足之处.假如,配置文件里面的字符串有一部分需要经常变动,另外一些不需要,上一章的方法就不方便了,所以这章主要讲如何在配置文件中使 ...

  6. PHP 根据key 给二维数组分组

    我们经常拿到一个二维数组出来,会发现结果和自己想要的有些偏差,可能需要根据二维数组里的某个字段对数组分组.先来看以下数组, Array ( [0] => Array ( [id] => 1 ...

  7. 23、ASP.NET MVC入门到精通——业务层和数据层父类及接口-T4模板

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在上一篇中,我们已经把项目的基本框架搭起来了,这一篇我们就来实现业务层和数据层的父接口及父类. 1.我们先来定义一个业务层父接口IBaseB ...

  8. 弄一个ajax笔记方便查询-$.ajax()

    $.ajax()是所有ajax方法中最底层的方法,所有其他方法都是基于$.ajax()方法的封装.这个方法只有一个参数,传递一个各个功能键值对的对象. $.ajax()方法对象参数表: 参数 类型 说 ...

  9. SharePoint 自定义的列表页面中添加javascript的一个 For循环语句后,该页面就打不开了。

    一个sharepoint 2013的普通的列表的自定义新建页面,我在其中新添加几行javascript代码后页面就打不开了.如图所示: 真是一言不合,友谊的页面说打不开就打不开啊.后来慢慢比对发现是因 ...

  10. Linux下tar-rar-unrar解压/压缩缩命令大全

    转载请标明出处: http://www.cnblogs.com/why168888/p/5975559.html 本文出自:[Edwin博客园] RAR文件下载:http://www.rarlab.c ...