SDN第三次上机作业

实验任务一

试验任务二

OpenFlow协议中交换机与控制器的消息交互过程如下图所示

Hello

首先,控制器与交互及互相发送 Hello 消息。

Features Request

其次,OpenFlow 连接建立之后,控制器需要获得交换机的特性信息,因此控制器向交换机发送 Features Request 消息查询交换机特性.

Features Reply

交换机在收到控制器发出的 Features Request 消息后,将自己的特性告诉给控制器,返回 Features Request 消息.

Set config

知道了交换机的特性之后就要配置交换机了。

Packet-in

有两种情况会触发交换机向控制器发送 Packet_in 消息

  • 1.数据包在交换机中匹配不到流表,则向controller发送Packet_in消息
  • 2.数据包在流表中有匹配的条目,但是其中所指示的 action 列表中包含转发给控制器的动作(Output = CONTROLLER)

    (注:该图是因为匹配不到流表,属于第一种)

Flow-Mod / Packet-out

当控制器收到 Packet-in 消息时有两种响应的方式:

  • Flow-Mod:控制器收到 Packet‐in 消息后,可以发送 Flow‐Mod 消息向交换机下发一个流表项。
  • Packet-out:与Flow-Mod不同的是,控制器不会下发流表,而是直接告诉交换机该如何做。

    (Flow-Mod)

    (Packet_out)

实验任务三

Q:交换机与控制器建立通信时是使用TCP协议还是UDP协议?TCP协议

进阶任务

OFPT_Hello

Hello包定义了一个header,header数据结构如下:

struct ofp_header {
    uint8_t version;    /* 版本号 */
    uint8_t type;       /* Openflow 协议类型. */
    uint16_t length;    /* Openflow 数据包包头. */
    uint32_t xid;       /* 数据包编号. */
};
struct ofp_hello {
    struct ofp_header header;
};

OFPT_ERROR

如果连接失败,会发送一个error包,error类型如下。

enum ofp_error_type {
    OFPET_HELLO_FAILED,         /* Hello protocol failed. */
    OFPET_BAD_REQUEST,          /* Request was not understood. */
    OFPET_BAD_ACTION,           /* Error in action description. */
    OFPET_FLOW_MOD_FAILED,      /* Problem modifying flow entry. */
    OFPET_PORT_MOD_FAILED,      /* Port mod request failed. */
    OFPET_QUEUE_OP_FAILED       /* Queue operation failed. */
};

OFPT_FEATURES

OFPT_FEATURES 主要是请求交换机的特性,而交换机的特性数据结构定义如下

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. */

    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. */
};

OFPT_PACKET_IN

OFPT_PACKET_IN产生的原因有两种,一种是没匹配到流表,另一种是匹配到了,动作是转发的控制器,代码如下

enum ofp_packet_in_reason {
    OFPR_NO_MATCH,          /* No matching flow. */
    OFPR_ACTION             /* Action explicitly output to controller. */
};

OFPT_PACKET_IN的数据格式如下:

struct ofp_packet_in {
    struct ofp_header header;
    uint32_t buffer_id;     /* ID assigned by datapath. */
    uint16_t total_len;     /* Full length of frame. */
    uint16_t in_port;       /* Port on which frame was received. */
    uint8_t reason;         /* Reason packet is being sent (one of OFPR_*) */
    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. */
};

OFPT_PACKET_OUT

packet_in事件之后,一般会触发两类事件,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). */
    uint16_t in_port;             /* Packet's input port (OFPP_NONE if none). */
    uint16_t actions_len;         /* Size of action array in bytes. */
    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.) */
};

OFPT_FLOW_MOD

Flow-Mod:控制器收到 Packet‐in 消息后,可以发送 Flow‐Mod 消息向交换机下发一个流表项。指导交换机转发数据包,其数据格式如下:

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*. */
    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. */
};
OFP_ASSERT(sizeof(struct ofp_flow_mod) == 72);

/* Why was this flow removed? */
enum ofp_flow_removed_reason {
    OFPRR_IDLE_TIMEOUT,         /* Flow idle time exceeded idle_timeout. */
    OFPRR_HARD_TIMEOUT,         /* Time exceeded hard_timeout. */
    OFPRR_DELETE                /* Evicted by a DELETE flow mod. */
};

个人心得

源码阅读其实有很多内容可写,如果把整个流程写出来,就太麻烦了。所以这边就非常简单写了一些OpenFlow主要消息类型对应的数据结构定义。还有交互过程也应该详细一些的。不过不太符合作业简要的要求,就没写太多。

原文地址:https://www.cnblogs.com/fcw245838813/p/15268968.html