SDN第三次上机作业
实验任务一
实验任务二
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 消息时有两种响应的方式:
1.Flow-Mod:控制器收到 Packet‐in 消息后,可以发送 Flow‐Mod 消息向交换机下发一个流表项
2.Packet-out:与Flow-Mod不同的是,控制器不会下发流表,而是直接告诉交换机该如何做
分析:OpenFlow协议中交换机与控制器的消息交互过程
实验任务三
交换机与控制器建立通信时是使用TCP协议
进阶任务
OFPT_Hello
Hello包定义了一个header
header数据结构:
Copy
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类型:
Copy
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. */
};
数据格式:
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。两者都是指导交换机如何处理数据包,区别是是否下发流表项
数据结构:
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
控制器收到 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. */
};
个人心得:
这次实验比前两次简单多了,无论是之前已经做过的搭建拓扑还是抓包都是比较容易的,也通过实验对上课的理论有了更深入的理解,也加深了印象。
主要遇到的问题是在进阶部分我不会解构数据包查看源数据包代码结构,所以这部分我参照了樊朝伟同学的博客,在SDN这部分懂得比较多,也希望之后还能够从他那边学到知识
(想要抓到完整的openflow数据包需要先打开wireshare再run你的拓扑,或者打开编译好的py文件;wireshare不知道抓什么接口可以直接抓any接口)