network arplab finished
This commit is contained in:
@@ -28,9 +28,11 @@
|
||||
#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);
|
||||
|
||||
|
||||
|
||||
@@ -498,6 +500,129 @@ 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消息
|
||||
*
|
||||
@@ -577,6 +702,8 @@ 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:
|
||||
@@ -592,6 +719,7 @@ 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
|
||||
|
||||
@@ -612,7 +740,8 @@ 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_PACKET_OUT | MASK_MULTIPART_REQUEST | MASK_ROLE_REQUEST | MASK_FLOW_MOD;
|
||||
|
||||
openflow_hook_init(mask,handle_openflow_callback);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user