network arp+icmp lab finished

This commit is contained in:
2025-11-18 14:46:26 +08:00
parent 9c6e429a47
commit e4c7bf757d
186 changed files with 316 additions and 16491 deletions

View File

@@ -26,17 +26,10 @@
#include <fast.h>
#include <main_libofp.h>
#include <ofp_v4.h>
#include <stdint.h>
#include <openboxS4.h>
#include <stddef.h>
extern void nms_exec_action(u32 inport,u32 outport,struct eth_header *eth,int len,int hit_idx);
extern void pkt_print(u8 *data, u16 len);
extern int fast_add_rule(struct fast_rule *rule);
extern uintptr_t *flow_stats_addr;
extern uint64_t *flow_stats_addr;
extern struct timeval flow_stats_time[];
extern struct nms_ports_info nmps;
extern struct timeval start_tv;
@@ -123,7 +116,7 @@ u8 *build_opfmsg_reply_ofpbuf(uint8_t type,uint32_t xid,uint16_t len)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_opfmsg_hello(struct ofp_buffer *ofpbuf)
{
@@ -136,7 +129,7 @@ handle_opfmsg_hello(struct ofp_buffer *ofpbuf)
int reply_len = sizeof(struct ofp_header);
//填充openflow协议头协议类型为OFPT_ERROR
struct ofp_buffer *ofpbuf_reply =
(struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_ERROR,ofpbuf->header.xid,reply_len);
(struct ofp_buffer *)build_reply_ofpbuf(OFPT_ERROR,ofpbuf->header.xid,reply_len);
//打印error消息
pkt_print((u8 *)ofpbuf,htons(ofpbuf->header.length));
//发送error消息
@@ -156,7 +149,7 @@ handle_opfmsg_hello(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_opfmsg_features_request(struct ofp_buffer *ofpbuf)
{
int feature_reply_len = sizeof(struct ofp_switch_features)+sizeof(struct ofp_header);
@@ -169,7 +162,7 @@ handle_opfmsg_features_request(struct ofp_buffer *ofpbuf)
//TODO
/* 构建feature回应报文把交换机的功能信息发送给控制器指针变量feature_reply_msg赋值 */
feature_reply_msg->datapath_id = htonll(0x1234ULL << 48);
feature_reply_msg->datapath_id = htonll(0x1234 << 48);
feature_reply_msg->n_buffers = 0x100; // 256
feature_reply_msg->n_tables = 1;
feature_reply_msg->auxiliary_id = 0;
@@ -191,12 +184,12 @@ handle_opfmsg_features_request(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_get_config_request(struct ofp_buffer *ofpbuf)
{
int reply_len = sizeof(struct ofp_switch_config)+sizeof(struct ofp_header);
//填充openflow协议头协议类型为 OFPT_GET_CONFIG_REPLY
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_GET_CONFIG_REPLY,
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_GET_CONFIG_REPLY,
ofpbuf->header.xid,reply_len);
struct ofp_switch_config *switch_config_reply =(struct ofp_switch_config *)ofpbuf_reply->data;
@@ -216,12 +209,12 @@ handle_ofpmsg_get_config_request(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_desc(struct ofp_buffer *ofpbuf)
{
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_multipart)+sizeof(struct ofp_desc_stats);
struct ofp_buffer *ofpbuf_reply =
(struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY,ofpbuf->header.xid,reply_len);
(struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY,ofpbuf->header.xid,reply_len);
struct ofp_multipart *ofpmp_reply = (struct ofp_multipart *)ofpbuf_reply->data;
static const char *default_mfr_desc = "Chengjingyu_Networks";
@@ -232,13 +225,13 @@ handle_ofpmsg_desc(struct ofp_buffer *ofpbuf)
ofpmp_reply->type = htons(OFPMP_DESC);
ofpmp_reply->flags = htonl(OFPMP_REPLY_MORE_NO);
snprintf(ofpmp_reply->ofpmp_desc[0].mfr_desc, sizeof ofpmp_reply->ofpmp_desc[0].mfr_desc, "%s", default_mfr_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].hw_desc, sizeof ofpmp_reply->ofpmp_desc[0].hw_desc, "%s", default_hw_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].sw_desc, sizeof ofpmp_reply->ofpmp_desc[0].sw_desc, "%s", default_sw_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].serial_num, sizeof ofpmp_reply->ofpmp_desc[0].serial_num, "%s", default_serial_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].dp_desc, sizeof ofpmp_reply->ofpmp_desc[0].dp_desc, "%s", default_dp_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].mfr_desc, sizeof ofpmp_reply->ofpmp_desc[0].mfr_desc, "%s", default_mfr_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].hw_desc, sizeof ofpmp_reply->ofpmp_desc[0].hw_desc, "%s", default_hw_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].sw_desc, sizeof ofpmp_reply->ofpmp_desc[0].sw_desc, "%s", default_sw_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].serial_num, sizeof ofpmp_reply->ofpmp_desc[0].serial_num, "%s", default_serial_desc);
snprintf(ofpmp_reply->ofpmp_desc[0].dp_desc, sizeof ofpmp_reply->ofpmp_desc[0].dp_desc, "%s", default_dp_desc);
send_openflow_message(ofpbuf_reply,reply_len);
return HANDLE;
}
@@ -251,42 +244,24 @@ handle_ofpmsg_desc(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_flow_stats(struct ofp_buffer *ofpbuf)
{
int i = 0, reply_len = 0, flow_stats_offset;
struct ofp_flow_stats *current_flow_stats = NULL;
struct ofp_flow_stats *flow_stats_ptr = NULL;
struct ofp_buffer *reply_buffer = NULL;
struct ofp_multipart *multipart_reply = NULL;
uint16_t flow_length = 0;
// 计算长度
for (i = 0; i < FAST_RULE_CNT; i++) {
for (; i < FAST_RULE_CNT; i++) {
if (flow_stats_addr[i] != 0) {
flow_stats_ptr = (struct ofp_flow_stats *)flow_stats_addr[i];
// 添加空指针检查和长度有效性检查
if (flow_stats_ptr == NULL) {
continue;
}
flow_length = ntohs(flow_stats_ptr->length);
// 检查长度是否合理(至少应该包含 ofp_flow_stats 结构体大小)
if (flow_length < sizeof(struct ofp_flow_stats) || flow_length > 65535) {
printf("Warning: Invalid flow stats length %d at index %d\n", flow_length, i);
continue;
}
reply_len += flow_length;
reply_len += ntohs(((struct ofp_flow_stats *)flow_stats_addr[i])->length);
}
}
reply_len += sizeof(struct ofp_header) + sizeof(struct ofp_multipart);
// 构造响应包
reply_buffer = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY, ofpbuf->header.xid, reply_len);
if (reply_buffer == NULL) {
printf("Error: Failed to allocate reply buffer\n");
return HANDLE;
}
multipart_reply = (struct ofp_multipart *)reply_buffer->data;
multipart_reply->type = htons(OFPMP_FLOW); // 标识信息
multipart_reply->flags = htonl(OFPMP_REPLY_MORE_NO); //这条不用回
@@ -297,23 +272,8 @@ handle_ofpmsg_flow_stats(struct ofp_buffer *ofpbuf)
for (i = 0; i < FAST_RULE_CNT; i++) {
if (flow_stats_addr[i] != 0) {
flow_stats_ptr = (struct ofp_flow_stats *)flow_stats_addr[i];
// 添加空指针检查
if (flow_stats_ptr == NULL) {
continue;
}
flow_length = ntohs(flow_stats_ptr->length);
// 再次检查长度有效性
if (flow_length < sizeof(struct ofp_flow_stats) || flow_length > 65535) {
continue;
}
// 检查是否会超出缓冲区边界
if (flow_stats_offset + flow_length > reply_len) {
printf("Error: Buffer overflow prevented at index %d\n", i);
break;
}
memcpy(current_flow_stats, (void *)flow_stats_ptr, flow_length);
flow_stats_offset += flow_length;
memcpy(current_flow_stats, (void *)flow_stats_addr[i], ntohs(((struct ofp_flow_stats *)flow_stats_addr[i])->length));
flow_stats_offset += ntohs(current_flow_stats->length);
current_flow_stats = (struct ofp_flow_stats *)&reply_buffer->data[flow_stats_offset];
}
}
@@ -322,6 +282,43 @@ handle_ofpmsg_flow_stats(struct ofp_buffer *ofpbuf)
return HANDLE;
}
// handle_ofpmsg_flow_stats(struct ofp_buffer *ofpbuf)
// {
// int i = 0, reply_len = 0, flow_stats_offset;
// struct ofp_flow_stats *current_flow_stats = NULL;
// struct ofp_buffer *reply_buffer = NULL;
// struct ofp_multipart *multipart_reply = NULL;
//
// // 计算长度
// for (; i < FAST_RULE_CNT; i++) {
// if (flow_stats_addr[i] != NULL) {
// reply_len += ntohs(((struct ofp_flow_stats *)flow_stats_addr[i])->length);
// }
// }
// reply_len += sizeof(struct ofp_header) + sizeof(struct ofp_multipart);
//
// // 构造响应包
// reply_buffer = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY, ofpbuf->header.xid, reply_len);
// multipart_reply = (struct ofp_multipart *)reply_buffer->data;
// multipart_reply->type = htons(OFPMP_FLOW); // 标识信息
// multipart_reply->flags = htonl(OFPMP_REPLY_MORE_NO); //这条不用回
//
// // 填充包体
// flow_stats_offset = sizeof(struct ofp_multipart);
// current_flow_stats = (struct ofp_flow_stats *)&reply_buffer->data[flow_stats_offset];
//
// for (i = 0; i < FAST_RULE_CNT; i++) {
// if (flow_stats_addr[i] != NULL) {
// memcpy(current_flow_stats, flow_stats_addr[i], ntohs(((struct ofp_flow_stats *)flow_stats_addr[i])->length));
// flow_stats_offset += ntohs(current_flow_stats->length);
// current_flow_stats = (struct ofp_flow_stats *)&reply_buffer->data[flow_stats_offset];
// }
// }
//
// send_openflow_message(reply_buffer, reply_len);
//
// return HANDLE;
// }
/**
* @brief 处理OpenFlow Aggregate Stats Request消息
@@ -332,11 +329,11 @@ handle_ofpmsg_flow_stats(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_aggregate(struct ofp_buffer *ofpbuf)
{
int reply_len = sizeof(struct ofp_header)+ sizeof(struct ofp_multipart)+sizeof(struct ofp_aggregate_stats_reply);
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY,
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY,
ofpbuf->header.xid,reply_len);
struct ofp_multipart *ofpmp_reply = (struct ofp_multipart *)ofpbuf_reply->data;
int i = 0,flow_count = 0;
@@ -344,20 +341,22 @@ handle_ofpmsg_aggregate(struct ofp_buffer *ofpbuf)
struct timeval tv;
ofpmp_reply->type = htons(OFPMP_AGGREGATE);
ofpmp_reply->flags = htonl(OFPMP_REPLY_MORE_NO);
gettimeofday(&tv, NULL);
ofpmp_reply->flags = htonl(OFPMP_REPLY_MORE_NO);
gettimeofday(&tv, NULL);
for (; i < FAST_RULE_CNT; i++) {
if (flow_stats_addr[i] != 0) {
((struct ofp_flow_stats *)flow_stats_addr[i])->duration_sec = htonl(tv.tv_sec - flow_stats_time[i].tv_sec);
((struct ofp_flow_stats *)flow_stats_addr[i])->duration_nsec = htonl(tv.tv_usec - flow_stats_time[i].tv_usec);
// 从硬件寄存器读取数据包计数和字节计数
for (; i < FAST_RULE_CNT; i++) {
if (flow_stats_addr[i] != NULL) {
((struct ofp_flow_stats *)flow_stats_addr[i])->duration_sec = htonl(tv.tv_sec - flow_stats_time[i].tv_sec);
((struct ofp_flow_stats *)flow_stats_addr[i])->duration_nsec = htonl(tv.tv_usec - flow_stats_time[i].tv_usec);
// ((struct ofp_flow_stats *)flow_stats_addr[i])->packet_count = ((uint64_t)0x100); // 涉及硬件地址拼尽全力无法找到姑且填充256
// ((struct ofp_flow_stats *)flow_stats_addr[i])->byte_count = ((uint64_t)0x40000); // 涉及硬件地址拼尽全力无法找到姑且填充262144=256*1024
// 从硬件寄存器读取数据包计数和字节计数
((struct ofp_flow_stats *)flow_stats_addr[i])->packet_count = fast_reg_rd(FAST_OFP_FLOW_STATS_PKTS + i * sizeof(uint64_t));
((struct ofp_flow_stats *)flow_stats_addr[i])->byte_count = fast_reg_rd(FAST_OFP_FLOW_STATS_BYTES + i * sizeof(uint64_t));
packet_count += ((struct ofp_flow_stats *)flow_stats_addr[i])->packet_count;
byte_count += ((struct ofp_flow_stats *)flow_stats_addr[i])->byte_count;
flow_count++;
flow_count++;
}
}
ofpmp_reply->ofpmp_aggregate_reply[0].packet_count = packet_count;
@@ -379,14 +378,15 @@ handle_ofpmsg_aggregate(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_table(struct ofp_buffer *ofpbuf)
{
int i = 0;
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_multipart)+
sizeof(struct ofp_table_stats)*1;
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY,
ofpbuf->header.xid,reply_len); struct ofp_multipart *ofpmp_reply = (struct ofp_multipart *)ofpbuf_reply->data;
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_multipart)+
sizeof(struct ofp_table_stats)*1;
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY,
ofpbuf->header.xid,reply_len);
struct ofp_multipart *ofpmp_reply = (struct ofp_multipart *)ofpbuf_reply->data;
ofpmp_reply->type = htons(OFPMP_TABLE);
ofpmp_reply->flags = htonl(OFPMP_REPLY_MORE_NO);
@@ -399,7 +399,7 @@ handle_ofpmsg_table(struct ofp_buffer *ofpbuf)
}
send_openflow_message(ofpbuf_reply,reply_len);
return HANDLE;
return HANDLE;
}
/**
@@ -411,13 +411,13 @@ handle_ofpmsg_table(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_port_stats(struct ofp_buffer *ofpbuf)
{
int i = 0;
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_multipart)+
sizeof(struct ofp_port_stats)*nmps.cnt;
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY,
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY,
ofpbuf->header.xid,reply_len);
struct ofp_multipart *ofpmp_reply = (struct ofp_multipart *)ofpbuf_reply->data;
struct timeval tv;
@@ -445,13 +445,13 @@ handle_ofpmsg_port_stats(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_group_features(struct ofp_buffer *ofpbuf)
{
int i = 0;
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_group_features)+8;
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY,
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY,
ofpbuf->header.xid,reply_len);
struct ofp_group_features *group = (struct ofp_group_features *)ofpbuf_reply->data;
@@ -459,7 +459,7 @@ handle_ofpmsg_group_features(struct ofp_buffer *ofpbuf)
send_openflow_message(ofpbuf_reply,reply_len);
return HANDLE;
return HANDLE;
}
@@ -472,13 +472,13 @@ handle_ofpmsg_group_features(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_port_desc(struct ofp_buffer *ofpbuf)
{
int i = 0;
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_multipart)+
sizeof(struct ofp_port)*nmps.cnt;
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_MULTIPART_REPLY,
struct ofp_buffer *ofpbuf_reply = (struct ofp_buffer *)build_reply_ofpbuf(OFPT_MULTIPART_REPLY,
ofpbuf->header.xid,reply_len);
struct ofp_multipart *ofpmp_reply = (struct ofp_multipart *)ofpbuf_reply->data;
@@ -490,7 +490,7 @@ handle_ofpmsg_port_desc(struct ofp_buffer *ofpbuf)
}
send_openflow_message(ofpbuf_reply,reply_len);
return HANDLE;
return HANDLE;
}
/**
@@ -502,7 +502,7 @@ handle_ofpmsg_port_desc(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle_ofpmsg_packet_out(struct ofp_buffer *ofpbuf)
{
struct ofp_packet_out *out = (struct ofp_packet_out *)ofpbuf;
@@ -533,129 +533,6 @@ handle_ofpmsg_packet_out(struct ofp_buffer *ofpbuf)
return HANDLE;
}
/**
* @brief 处理OpenFlow Flow Mod消息
*
* 该函数用于处理接收到的OpenFlow Flow Mod消息并添加流表规则。
*
* @param ofpbuf 接收到的OpenFlow消息缓冲区
*
* @return 返回处理状态码
*/
// static int
// handle_ofpmsg_flow_mod(struct ofp_buffer *ofpbuf)
// {
// struct ofp_flow_mod *flow_mod = (struct ofp_flow_mod *)ofpbuf->data;
// struct fast_rule rule;
// int match_len;
// int oxm_offset;
// u8 *p;
// if (flow_mod->command != OFPFC_ADD) {
// printf("Unsupported flow_mod command: %d\n", flow_mod->command);
// return CONTINUE;
// }
// memset(&rule, 0, sizeof(struct fast_rule));
// rule.valid = 1;
// rule.priority = ntohs(flow_mod->priority);
// rule.cookie = ntohll(flow_mod->cookie);
// rule.cookie_mask = ntohll(flow_mod->cookie_mask);
// match_len = ntohs(flow_mod->match.length);
// p = (u8 *)flow_mod->match.oxm_idl;
// oxm_offset = 0;
// while (oxm_offset < match_len - 4) { // -4 for padding
// u16 oxm_class = ntohs(*(u16 *)(p + oxm_offset));
// u8 oxm_field_hm = *(p + oxm_offset + 2);
// u8 oxm_field = oxm_field_hm >> 1;
// u8 oxm_hasmask = oxm_field_hm & 1;
// u8 oxm_length = *(p + oxm_offset + 3);
// u8 *value = p + oxm_offset + 4;
// if (oxm_class == OFPXMC_OPENFLOW_BASIC) {
// switch (oxm_field) {
// case OFPXMT_OFB_IN_PORT:
// rule.key.port = ntohl(*(u32 *)value);
// rule.mask.port = 0x3f;
// break;
// case OFPXMT_OFB_ETH_DST:
// memcpy(rule.key.dmac, value, ETH_ALEN);
// if (oxm_hasmask) memcpy(rule.mask.dmac, value + ETH_ALEN, ETH_ALEN);
// else memset(rule.mask.dmac, 0xff, ETH_ALEN);
// break;
// case OFPXMT_OFB_ETH_SRC:
// memcpy(rule.key.smac, value, ETH_ALEN);
// if (oxm_hasmask) memcpy(rule.mask.smac, value + ETH_ALEN, ETH_ALEN);
// else memset(rule.mask.smac, 0xff, ETH_ALEN);
// break;
// case OFPXMT_OFB_ETH_TYPE:
// rule.key.type = ntohs(*(u16 *)value);
// if (oxm_hasmask) rule.mask.type = ntohs(*(u16 *)(value + 2));
// else rule.mask.type = 0xffff;
// break;
// case OFPXMT_OFB_IPV4_SRC:
// rule.key.ipv4.src = *(u32 *)value;
// if (oxm_hasmask) rule.mask.ipv4.src = *(u32 *)(value + 4);
// else rule.mask.ipv4.src = 0xffffffff;
// break;
// case OFPXMT_OFB_IPV4_DST:
// rule.key.ipv4.dst = *(u32 *)value;
// if (oxm_hasmask) rule.mask.ipv4.dst = *(u32 *)(value + 4);
// else rule.mask.ipv4.dst = 0xffffffff;
// break;
// case OFPXMT_OFB_IP_PROTO:
// rule.key.proto = *value;
// if (oxm_hasmask) rule.mask.proto = *(value + 1);
// else rule.mask.proto = 0xff;
// break;
// case OFPXMT_OFB_TCP_SRC:
// case OFPXMT_OFB_UDP_SRC:
// rule.key.ipv4.tp.sport = ntohs(*(u16 *)value);
// if (oxm_hasmask) rule.mask.ipv4.tp.sport = ntohs(*(u16 *)(value + 2));
// else rule.mask.ipv4.tp.sport = 0xffff;
// break;
// case OFPXMT_OFB_TCP_DST:
// case OFPXMT_OFB_UDP_DST:
// rule.key.ipv4.tp.dport = ntohs(*(u16 *)value);
// if (oxm_hasmask) rule.mask.ipv4.tp.dport = ntohs(*(u16 *)(value + 2));
// else rule.mask.ipv4.tp.dport = 0xffff;
// break;
// }
// }
// oxm_offset += 4 + oxm_length;
// }
// u8 *instructions_start = (u8 *)&flow_mod->match + ROUND_UP(ntohs(flow_mod->match.length), 8);
// int instructions_len = ntohs(ofpbuf->header.length) - (instructions_start - (u8 *)ofpbuf);
// int inst_offset = 0;
// while (inst_offset < instructions_len) {
// struct ofp_instruction *inst = (struct ofp_instruction *)(instructions_start + inst_offset);
// if (ntohs(inst->type) == OFPIT_APPLY_ACTIONS) {
// int action_offset = sizeof(struct ofp_instruction);
// while (action_offset < ntohs(inst->len)) {
// struct ofp_action *action = (struct ofp_action *)((u8 *)inst + action_offset);
// if (ntohs(action->type) == OFPAT_OUTPUT) {
// struct ofp_action_output *out_action = (struct ofp_action_output *)action;
// u32 out_port = ntohl(out_action->port);
// rule.action = ((u64)ACTION_PORT << 28) | out_port;
// }
// action_offset += ntohs(action->length);
// }
// }
// inst_offset += ntohs(inst->len);
// }
// if (fast_add_rule(&rule) < 0) {
// printf("Failed to add flow rule\n");
// } else {
// printf("Successfully added flow rule\n");
// }
// return HANDLE;
// }
/**
* @brief 处理OpenFlow Role Request消息
*
@@ -665,12 +542,12 @@ handle_ofpmsg_packet_out(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
static int
static enum ofperr
handle__opfmsg_role_request(struct ofp_buffer *ofpbuf)
{
int reply_len = sizeof(struct ofp_header)+sizeof(struct ofp_role);
struct ofp_buffer *ofpbuf_reply =
(struct ofp_buffer *)build_opfmsg_reply_ofpbuf(OFPT_ROLE_REPLY,ofpbuf->header.xid,reply_len);
(struct ofp_buffer *)build_reply_ofpbuf(OFPT_ROLE_REPLY,ofpbuf->header.xid,reply_len);
SHOW_FUN(0);
memcpy(ofpbuf_reply->data,ofpbuf->data,sizeof(struct ofp_role));
@@ -694,7 +571,7 @@ handle__opfmsg_role_request(struct ofp_buffer *ofpbuf)
*
* @return 返回处理状态码
*/
int handle_openflow_callback(struct ofp_buffer *ofpbuf, int len)
enum ofperr handle_openflow_callback(struct ofp_buffer *ofpbuf, int len)
{
int oftype = ofpbuf->header.type;
printf("header.version:%d, type:%d, len:%d\n", ofpbuf->header.version, ofpbuf->header.type, htons(ofpbuf->header.length));
@@ -725,8 +602,6 @@ int handle_openflow_callback(struct ofp_buffer *ofpbuf, int len)
return handle_ofpmsg_group_features(ofpbuf);
case OFPMP_PORT_DESC:
return handle_ofpmsg_port_desc(ofpbuf);
// case OFPT_FLOW_MOD:
// return handle_flow_mod(ofpbuf);
default:
printf("Unsupported multipart request type: %d\n", ntohs(multipart->type));
break;
@@ -735,8 +610,6 @@ int handle_openflow_callback(struct ofp_buffer *ofpbuf, int len)
break;
case OFPT_PACKET_OUT:
return handle_ofpmsg_packet_out(ofpbuf);
// case OFPT_FLOW_MOD:
// return handle_ofpmsg_flow_mod(ofpbuf);
case OFPT_ROLE_REQUEST:
return handle__opfmsg_role_request(ofpbuf);
default:
@@ -752,7 +625,6 @@ int handle_openflow_callback(struct ofp_buffer *ofpbuf, int len)
#define MASK_FEATURES_REQUEST (1 << OFPT_FEATURES_REQUEST) // 1 << 5
#define MASK_GET_CONFIG_REQUEST (1 << OFPT_GET_CONFIG_REQUEST) // 1 << 7
#define MASK_PACKET_OUT (1 << OFPT_PACKET_OUT) // 1 << 13
// #define MASK_FLOW_MOD (1 << OFPT_FLOW_MOD) // 1 << 14
#define MASK_MULTIPART_REQUEST (1 << OFPT_MULTIPART_REQUEST) // 1 << 18
#define MASK_ROLE_REQUEST (1 << OFPT_ROLE_REQUEST) // 1 << 24
@@ -773,8 +645,7 @@ int main(int argc,char* argv[])
// OFPT_HELLO, OFPT_FEATURES_REQUEST, OFPT_GET_CONFIG_REQUEST,
// OFPT_PACKET_OUT, OFPT_MULTIPART_REQUEST, OFPT_ROLE_REQUEST
mask = MASK_HELLO | MASK_FEATURES_REQUEST | MASK_GET_CONFIG_REQUEST |
MASK_PACKET_OUT | MASK_MULTIPART_REQUEST | MASK_ROLE_REQUEST ;
// MASK_PACKET_OUT | MASK_MULTIPART_REQUEST | MASK_ROLE_REQUEST | MASK_FLOW_MOD;
MASK_PACKET_OUT | MASK_MULTIPART_REQUEST | MASK_ROLE_REQUEST;
openflow_hook_init(mask,handle_openflow_callback);