首页 > 其他分享 >实验3:OpenFlow协议分析实践

实验3:OpenFlow协议分析实践

时间:2022-09-28 13:33:53浏览次数:76  
标签:控制器 struct OpenFlow 实践 header ofp 交换机 实验 net

(一)基本要求

1.搭建下图所示拓扑,完成相关 IP 配置,并实现主机与主机之间的 IP 通信。用抓包软件获取控制器与交换机之间的通信数据。


a)代码如下:

点击查看代码
#!/usr/bin/env python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='192.168.0.100/24')

    info( '*** Adding controller\n' )
    c0=net.addController(name='c0',
                      controller=Controller,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='192.168.0.101/24', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='192.168.0.102/24', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='192.168.0.103/24', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='192.168.0.104/24', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, s1, 1, 1)
    net.addLink(s1, h2, 2, 1)
    net.addLink(s1, s2, 3, 3)
    net.addLink(s2, h3, 1, 1)
    net.addLink(s2, h4, 2, 1)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')
    net.get('s1').start([c0])
    net.get('s2').start([c0])
    
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

b)抓包结果分析

  • hello
    • 控制器6633端口(我最高能支持OpenFlow 1.0) ---> 交换机43130端口
    • 交换机43130端口(我最高能支持OpenFlow 1.5) ---> 控制器6633端口
  • Features_Request / Set_Config
    • 控制器6633端口(我需要你的特征信息) ---> 交换机43130端口
    • 控制器6633端口(请按照我给你的flag和max bytes of packet进行配置) ---> 交换机43130端口
  • Port_Status
    • 从交换机43130端口到控制器6633端口当交换机端口发生变化时,告知控制器相应的端口状态
  • Features_Reply
    • 交换机43130端口(这是我的特征信息,请查收) ---> 控制器6633端口
  • Packet_in
    • 交换机43130端口(有数据包进来,请指示)--- 控制器6633端口
    • 发现是因为交换机发现此时自己并没有匹配的流表(Reason: No matching flow (table-miss flow entry) (0)),所以要问控制器如何处理
  • Packet_out
    • 控制器6633端口(请按照我给你的action进行处理,输出到66531端口) ---> 交换机43130端口
  • Flow_mod
    • 控制器通过6633端口向交换机431030端口下发流表项,指导数据的转发处理

2.查看抓包结果,分析OpenFlow协议中交换机与控制器的消息交互过程,画出相关交互图或流程图。

3.回答问题:交换机与控制器建立通信时是使用TCP协议还是UDP协议?

TCP

(二)进阶要求

将抓包基础要求第2步的抓包结果对照OpenFlow源码,了解OpenFlow主要消息类型对应的数据结构定义。

  • hello
点击查看代码
struct ofp_header {
    uint8_t version;    /* OFP_VERSION. */
    uint8_t type;       /* One of the OFPT_ constants. */
    uint16_t length;    /* Length including this ofp_header. */
    uint32_t xid;       /* Transaction id associated with this packet.
                           Replies use the same id as was in the request
                           to facilitate pairing. */
};
OFP_ASSERT(sizeof(struct ofp_header) == 8);

/* OFPT_HELLO.  This message has an empty body, but implementations must
 * ignore any data included in the body, to allow for future extensions. */
struct ofp_hello {
    struct ofp_header header;
};


控制器与交互及互相发送 Hello 消息。Hello消息中只包含有ofp_header,包括:version,type,length,transaction ID,其中的version 字段为发送方所支持的最高版本 OpenFlow,type字段为OFPT_HELLO。
数据结构示意图如下:

  • Features_Request

控制器向交换机发送 Features_Request 消息查询交换机特性,Features_Request消息只包含ofp_header,其中type字段为 OFPT_FEATURES_REQUEST。
数据结构示意图如下:

  • Features_Reply
点击查看代码
struct ofp_phy_port {
    uint16_t port_no;
    uint8_t hw_addr[OFP_ETH_ALEN];
    char name[OFP_MAX_PORT_NAME_LEN]; /* Null-terminated */

    uint32_t config;        /* Bitmap of OFPPC_* flags. */
    uint32_t state;         /* Bitmap of OFPPS_* flags. */

    /* Bitmaps of OFPPF_* that describe features.  All bits zeroed if
     * unsupported or unavailable. */
    uint32_t curr;          /* Current features. */
    uint32_t advertised;    /* Features being advertised by the port. */
    uint32_t supported;     /* Features supported by the port. */
    uint32_t peer;          /* Features advertised by peer. */
};
    
    struct ofp_switch_features {
    struct ofp_header header;
    uint64_t datapath_id;   /* Datapath unique ID.  The lower 48-bits are for
                               a MAC address, while the upper 16-bits are
                               implementer-defined. */
                            /*唯一标识 id 号*/
    uint32_t n_buffers;     /* Max packets buffered at once. */ /*交缓冲区可以缓存的最大数据包个数*/
                            
    uint8_t n_tables;       /* Number of tables supported by datapath. */ /*流表数量*/
                          
    uint8_t pad[3];         /* Align to 64-bits. */ /*填充值*/
                            
    /* Features. */
    uint32_t capabilities;  /* Bitmap of support "ofp_capabilities". */ /*支持的特殊功能*/
    uint32_t actions;       /* Bitmap of supported "ofp_action_type"s. */ /*支持的动作*/

    /* Port info.*/
    struct ofp_phy_port ports[0];  /* Port definitions.  The number of ports
                                      is inferred from the length field in
                                      the header. */ /*物理端口描述列表*/
};

交换机在收到控制器发出的 Features Request 消息后返回 Features Request 消息,Features 消息包括ofp_header和交换机的特性信息。
数据结构示意图如下:

  • Set_config
点击查看代码
struct ofp_switch_config {
    struct ofp_header header;
    uint16_t flags;             /* OFPC_* flags. */ /*指示交换机如何才处理 IP 分片数据包*/
    uint16_t miss_send_len;     /* Max bytes of new flow that datapath should
                                   send to the controller. */ /*指示当一个交换机无法处理的数据包到达时,将数据包发给控制器的最大字节数*/
};
enum ofp_config_flags {
    /* Handling of IP fragments. */
    OFPC_FRAG_NORMAL   = 0,  /* No special handling for fragments. */
    OFPC_FRAG_DROP     = 1,  /* Drop fragments. */
    OFPC_FRAG_REASM    = 2,  /* Reassemble (only if OFPC_IP_REASM set). */
    OFPC_FRAG_MASK     = 3
};

控制器了解交换机的特性后对交换机的 flags 和 miss_send_len 两个属性进行配置。
数据结构示意图如下:

  • Port_Status
点击查看代码
struct ofp_port_status {
    struct ofp_header header;
    uint8_t reason;          /* One of OFPPR_*. */
    uint8_t pad[7];          /* Align to 64-bits. */
    struct ofp_phy_port desc;
};

数据结构示意图如下:

  • Packet_in
点击查看代码
struct ofp_packet_in {
    struct ofp_header header;
    uint32_t buffer_id;     /* ID assigned by datapath. */ /*Packet-in消息所携带的数据包在交换机缓存区中的ID*/
    uint16_t total_len;     /* Full length of frame. */ /*data字段的长度*/
    uint16_t in_port;       /* Port on which frame was received. */ /*数据包进入交换机时的端口号*/
    uint8_t reason;         /* Reason packet is being sent (one of OFPR_*) */ /*发送Packet-in消息的原因*/
    uint8_t pad;
    uint8_t data[0];        /* Ethernet frame, halfway through 32-bit word,
                               so the IP header is 32-bit aligned.  The
                               amount of data is inferred from the length
                               field in the header.  Because of padding,
                               offsetof(struct ofp_packet_in, data) ==
                               sizeof(struct ofp_packet_in) - 2. */ /*携带的数据包*/
};
enum ofp_packet_in_reason {
    OFPR_NO_MATCH,          /* No matching flow. */ /*没有匹配的条目*/
    OFPR_ACTION             /* Action explicitly output to controller. */ /*action列表中包含转发给控制器的动作*/
};

有两种情况会触发交换机向控制器发送 Packet-in 消息:
1.交换机查找流表,发现没有匹配条目时。
2.有匹配条目但是对应的action是OUTPUT=CONTROLLER时。(截图中可知(Reason: No matching flow (table-miss flow entry) (0)),所以要问控制器如何处理)
数据结构示意图如下:

  • Packet_out / Flow_mod

Packet_out

点击查看代码
struct ofp_packet_out {
  struct ofp_header header;
  uint32_t buffer_id;           /* ID assigned by datapath (-1 if none). */ /*交换机缓存区id*/
  uint16_t in_port;             /* Packet's input port (OFPP_NONE if none). */
  uint16_t actions_len;         /* Size of action array in bytes. */ /*action列表的长度*/
  struct ofp_action_header actions[0]; /* Actions. */ /*动作列表*/
  /* uint8_t data[0]; */        /* Packet data.  The length is inferred
                                   from the length field in the header.
                                   (Only meaningful if buffer_id == -1.) */
};
struct ofp_action_header {
  uint16_t type;                  /* One of OFPAT_*. */
  uint16_t len;                   /* Length of action, including this
                                     header.  This is the length of action,
                                     including any padding to make it
                                     64-bit aligned. */
  uint8_t pad[4];
};


Flow_mod

点击查看代码
enum ofp_flow_mod_command /*Flow-Mod 消息共有五种类型*/
 {
    OFPFC_ADD,              /* New flow. */
    OFPFC_MODIFY,           /* Modify all matching flows. */
    OFPFC_MODIFY_STRICT,    /* Modify entry strictly matching wildcards */
    OFPFC_DELETE,           /* Delete all matching flows. */
    OFPFC_DELETE_STRICT    /* Strictly match wildcards and priority. */
};
struct ofp_flow_mod {
    struct ofp_header header;
    struct ofp_match match;      /* Fields to match */ /*流表的匹配域*/
    uint64_t cookie;             /* Opaque controller-issued identifier. */ /*流表项标识符*/

    /* Flow actions. */
    uint16_t command;             /* One of OFPFC_*. */
    uint16_t idle_timeout;        /* Idle time before discarding (seconds). */ /*空闲超时时间*/
    uint16_t hard_timeout;        /* Max time before discarding (seconds). */  /*最大生存时间*/
    uint16_t priority;            /* Priority level of flow entry. */ /*优先级,优先级高的流表项优先匹配*/
    uint32_t buffer_id;           /* Buffered packet to apply to (or -1). 
                                     Not meaningful for OFPFC_DELETE*. */ /*缓存区ID*/
    uint16_t out_port;            /* For OFPFC_DELETE* commands, require
                                     matching entries to include this as an
                                     output port.  A value of OFPP_NONE
                                     indicates no restriction. */
    uint16_t flags;               /* One of OFPFF_*. */
    struct ofp_action_header actions[0]; /* The action length is inferred
                                            from the length field in the
                                            header. */ /*action列表*/
};

当控制器收到 Packet-in 消息时有两种响应的方式,可以发送 Flow‐Mod 消息向交换机写一个流表项,控制器向交换机写入了一条与数据包相关的流表项,并且指定该数据包按照此流表项的 action 列表处理;网络中有些数据包出现的数量很少(如ARP、 IGMP等),没必要通过流表项来指定这一类数据包的处理方法。此时可以使用 Packet-Out 消息,告诉交换机某一个数据包如何处理。
数据结构示意图如下:

(三)参考资料

https://www.sdnlab.com/sdn-guide/14797.html
https://www.cnblogs.com/multhree/p/9520859.html

(四)个人总结

这次的实验内容只要按照实验内容指引按部就班操作就可以完成,难度不大。难点在于自身对于各个数据的定义与用途不太了解,虽然完成了实验,但具体的知识还是一知半解。在查阅了相关资料后勉强懂得各项数据的具体意义。总的来说这次最大的收获是能了解到OPENFLOW协议下控制器与交换机的交互流程,细节上的知识仍需加深记忆。

标签:控制器,struct,OpenFlow,实践,header,ofp,交换机,实验,net
From: https://www.cnblogs.com/fatty419/p/16737721.html

相关文章

  • 实验3:OpenFlow协议分析实践
    一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。二、实验环境Ubuntu20.0......
  • IM跨平台技术学习(三):vivo的Electron技术栈选型、全方位实践总结
    本文由vivo技术团队YangKun分享,原题“electron应用开发优秀实践”,本文有修订。1、引言在上篇《Electron初体验(快速开始、跨进程通信、打包、踩坑等)》的分享中,我们已......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验三:OpenFlow协议分析实践
    实验三:OpenFlow协议分析实践一、实验目的1、能够运用wireshark对OpenFlow协议数据交互过程进行抓包;2、能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程......
  • 实验3:OpenFlow协议分析实践
    @目录一、实验目的二、实验环境三、实验要求(一)基本要求1、搭建下图所示拓扑,完成相关IP配置,并实现主机与主机之间的IP通信。ip和拓扑2、查看抓包结果,分析OpenFlow协议中......
  • 实验3:OpenFlow协议分析实践
    一、实验目的1.能够运用wireshark对OpenFlow协议数据交互过程进行抓包;2.能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。二、实验环境Ubuntu......
  • 实验3:OpenFlow协议分析实践
    基础要求一、拓扑文件#!/usr/bin/envpythonfrommininet.netimportMininetfrommininet.nodeimportController,RemoteController,OVSControllerfrommininet.......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。......
  • 实验3:OpenFlow协议分析实践
    一.实验目的1.能够运用wireshark对OpenFlow协议数据交互过程进行抓包;2.能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。二.实验环境Ubuntu20......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践(一)基本要求1.搭建下图所示拓扑,完成相关IP配置,并实现主机与主机之间的IP通信。代码部分#!/usr/bin/envpythonfrommininet.......