• 13 netifd


    1 简介

    netifd管理网络接口和路由的后台进程。它与内核通信采用Netlink接口操作。
    Netlink是内核空间和用户空间的通信机制
    netifd主要包含设备和接口对象。设备代表linux物理接口或者一个虚拟链路接口。例:eth0 ppp等。


    设备类型
    simple_device_type  : 简单设备
    bridge_device_type  : 网桥设备
    tunnel_device_type  : 隧道设备
    macvlan_device_type : 在物理网卡上创建另外一个虚拟网卡
    vlandev_device_type : 物理网卡通过vlanid划分为多个网卡
    

    2 netifd方法

    netifdubus中注册了三种对象network network.device network.interface

    2.1 network的对象方法

    restart     : netifd_handle_restart
            重启
    reload      : netifd_handle_load
            重新读取配置来初始化网络设备
    add_host_route : netifd_add_host_route
            增加静态主机路由
            ubus call network add_host_route '{"target":"192.168.1.20","v6":"false"}'
    get_proto_handlers : netifd_get_proto_handlers
            获取系统支持的协议处理函数
    

    2.2 network.device对象方法

    network.device是一个二层设备接口

    status      : netifd_dev_status
            获取物理网卡的状态
            ubus call network.device status '{"name":"eth0"}'
    set_alias   : netifd_handle_alias
            设置alias
    set_state   : netifd_handle_set_state
            设置状态
    

    2.3 network.intface对象方法

    network.intface是一个三层接口,可以包含多个二层网卡设备

    up          : netifd_handle_up
                启动接口
    down        : netifd_handle_down
                关闭接口
    status      : netifd_handle_status
                查看接口状态
    add_device  : netifd_iface_handle_device
                增加设备
    remove_device : netifd_iface_handle_device
                删除设备
    notify_proto  : netifd_iface_notify_proto
                调用原型函数
    remove      : netifd_iface_remove
                删除接口
    set_data    : netifd_handle_set_data
                设置额外的存储数据
                ubus call network.interface.lan status
    
    notify_proto注册的shell命令
    • proto_init_update
      初始化设备及配置
    • proto_run_command
      运行获取IP地址命令
    • proto_kill_command
      杀掉协议处理进程
    • proto_notify_error
      通知发生错误
    • proto_block_restart
      设置自动启动标示变量为autostartfalse
    • proto_set_available
      设置接口available
    • proto_add_host_dependency
      添加对端IP地址的路由
    • proto_setup_failed
      失败后设置状态

    编号为netifd进程和shell脚本。预先在netifd-proto.sh中设置好的。
    DHCP处理过程中首先会调用proto_init_uodate函数来初始化设备。初始化完成之后通过proto_run_command命令来启动udhcp进程获取IP地址信息。
    每一种的协议处理的脚本都放在/lib/netifd/proto目录下。文件名通常和网络配置文件network中的协议选项关联起来。

    :~# cat /lib/netifd/proto/dhcp.sh 
    #!/bin/sh
    
    . /lib/functions.sh     #导入通用shell脚本
    . ../netifd-proto.sh    #导入通用shell脚本
    
    init_proto "$@"
    
    ### 协议配置初始化,让netifd知道这个协议所拥有的参数,存储在/etc/config/network配置文件中
    proto_dhcp_init_config() {
            renew_handler=1
    
            proto_config_add_string 'ipaddr:ipaddr'
            proto_config_add_string 'hostname:hostname'
            proto_config_add_string clientid
            proto_config_add_string vendorid
            proto_config_add_boolean 'broadcast:bool'
            proto_config_add_string 'reqopts:list(string)'
            proto_config_add_string iface6rd
            proto_config_add_string sendopts
            proto_config_add_boolean delegate
            proto_config_add_string zone6rd
            proto_config_add_string zone
            proto_config_add_string mtu6rd
            proto_config_add_string customroutes
    }
    
    ### 协议的设置和接口启动。入参:配置节名称,接口名称。
    ### 作用:读取配置文件中的参数,然后将参数传递给netifd
    proto_dhcp_setup() {
            local config="$1"
            local iface="$2"
    
            local ipaddr hostname clientid vendorid broadcast reqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes netmask
            json_get_vars ipaddr hostname clientid vendorid broadcast reqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes netmask
    
            local opt dhcpopts
            for opt in $reqopts; do
                    append dhcpopts "-O $opt"
            done
    
            for opt in $sendopts; do
                    append dhcpopts "-x $opt"
            done
    
            [ "$broadcast" = 1  -o -z "$broadcast" ] && broadcast="-B" || broadcast=
            [ -n "$clientid" ] && clientid="-x 0x3d:${clientid//:/}" || clientid="-C"
            [ -n "$iface6rd" ] && proto_export "IFACE6RD=$iface6rd"
            [ "$iface6rd" != 0 -a -f /lib/netifd/proto/6rd.sh ] && append dhcpopts "-O 212"
            [ -n "$zone6rd" ] && proto_export "ZONE6RD=$zone6rd"
            [ -n "$zone" ] && proto_export "ZONE=$zone"
            [ -n "$mtu6rd" ] && proto_export "MTU6RD=$mtu6rd"
            [ -n "$customroutes" ] && proto_export "CUSTOMROUTES=$customroutes"
            [ "$delegate" = "0" ] && proto_export "IFACE6RD_DELEGATE=0"
            [ -z "$hostname" ] && hostname=$(uci get system.@system[0].hostname)
    
            proto_export "INTERFACE=$config"
            proto_run_command "$config" udhcpc \
                    -p /var/run/udhcpc-$iface.pid \
                    -s /lib/netifd/dhcp.script \
                    -f -t 0 -i "$iface" \
                    ${ipaddr:+-r $ipaddr} \
                    ${hostname:+-H $hostname} \
                    ${vendorid:+-V $vendorid} \
                    $clientid $broadcast $dhcpopts
    
            #set fallback ip
            ifconfig $iface $ipaddr netmask $netmask
    }
    
    proto_dhcp_renew() {
            local interface="$1"
            # SIGUSR1 forces udhcpc to renew its lease
            local sigusr1="$(kill -l SIGUSR1)"
            [ -n "$sigusr1" ] && proto_kill_command "$interface" $sigusr1
    }
    
    ### 接口关闭。入参为uci配置节名称
    proto_dhcp_teardown() {
            local interface="$1"
            proto_kill_command "$interface"
    }
    
    

    2.4 netifd文件

    :~# /sbin/if
    ifconfig  ifdown    ifstatus  ifup
    

    ifupifdown实际上是东一个文件ifdown是指向ifup的软链接。在执行时会判断执行的文件名称。然后传递相应的参数
    这些命令的实质是调用ubus来实现的

    2.5 网络配置

    配置文件在/etc/config/network中。配置文件定义了二层网络设备和网络接口,路由和策略等。

    interface静态配置选项
    ifname      : 物理网卡接口名称
    type        :网络类型:如bridge
    proto       :协议配置:static
    ipaddr      :ip地址
    netmask     :网络掩码
    dns         :域名服务器地址:如8.8.8.8
    mtu         :设置接口的mtu地址
    
    interface DHCP常见配置选项
    ifname      : 设备接口名称。eth0
    proto       : 协议类型为DHCP
    hostname    : DHCP请求中的主机名,可以不用设置
    vendorid    : DHCP请求中的厂商ID,可以不用设置
    ipaddr      : 建议ip地址,可以不用设置
    
    pppoe使用用户名和,密码进行宽带拨号上网
    ifname      : pppoe使用物理网卡接口名称
    proto       : 协议为pppoe,采用点对点拨号连接
    username    : PAP或CHAP认证用户名
    password    : PAP或CHAP认证密码
    demand      : 指定空闲时间之后将连接关闭
    
  • 相关阅读:
    以淘宝商品搜索漫谈查询条件的排序对效率的影响(SQL查询性能优化,附调优(性能诊断)DMV)
    监测ASP.NET MVC网站
    在WCF中使用Ninject轻量级IOC框架 之 SOAP风格服务
    敏捷——SCRUM
    《scrum敏捷软件开发》读书笔记
    【双旦献礼】PortalBasic Java Web 应用开发框架 v3.0.1 正式发布(源码、示例及文档)
    前端架构
    Android源码学习之如何使用eclipse+NDK
    mass Framework attr模块 v3
    【转】iOS 6版本与之前版本差异总结
  • 原文地址:https://www.cnblogs.com/burnk/p/15822152.html
Copyright © 2020-2023  润新知