// Top-level probe header, indicates how many hops this probe // packet has traversed so far. //顶层的探测header,指出这个探测包经过了多少跳 header probe_t { bit<8> hop_cnt; }
// The data added to the probe by each switch at each hop. //每一个交换机加到探测包上面的数据 header probe_data_t { bit<1> bos;//bottom of stack bit<7> swid;//switch ID bit<8> port; bit<32> byte_cnt;//和下面的寄存器应该对应 time_t last_time;//和下面的寄存器应该对应 time_t cur_time; }
// Indicates the egress port the switch should send this probe // packet out of. There is one of these headers for each hop. //指示交换机应该发送该探测报文的出口端口。每个跳跃都有一个这样的 header header probe_fwd_t { bit<8> egress_spec; }
/************************************************************************* *********************** H E A D E R S *********************************** *************************************************************************/
// Top-level probe header, indicates how many hops this probe // packet has traversed so far. header probe_t { bit<8> hop_cnt; }
// The data added to the probe by each switch at each hop. header probe_data_t { bit<1> bos; bit<7> swid; bit<8> port; bit<32> byte_cnt; time_t last_time; time_t cur_time; }
// Indicates the egress port the switch should send this probe // packet out of. There is one of these headers for each hop. header probe_fwd_t { bit<8> egress_spec; }
/************************************************************************* *********************** P A R S E R *********************************** *************************************************************************/
/************************************************************************* ************ C H E C K S U M V E R I F I C A T I O N ************* *************************************************************************/
/************************************************************************* ************** I N G R E S S P R O C E S S I N G ******************* *************************************************************************/
/************************************************************************* **************** E G R E S S P R O C E S S I N G ******************** *************************************************************************/
// count the number of bytes seen since the last probe register<bit<32>>(MAX_PORTS) byte_cnt_reg; // remember the time of the last probe register<time_t>(MAX_PORTS) last_time_reg;
/************************************************************************* **************** E G R E S S P R O C E S S I N G ******************** *************************************************************************/
// count the number of bytes seen since the last probe register<bit<32>>(MAX_PORTS) byte_cnt_reg; // remember the time of the last probe register<time_t>(MAX_PORTS) last_time_reg;
/************************************************************************* ************* C H E C K S U M C O M P U T A T I O N *************** *************************************************************************/
/************************************************************************* *********************** D E P A R S E R ******************************* *************************************************************************/
control MyDeparser(packet_out packet, in headers hdr) { apply { packet.emit(hdr.ethernet); packet.emit(hdr.ipv4); packet.emit(hdr.probe); packet.emit(hdr.probe_data); packet.emit(hdr.probe_fwd); } }
/************************************************************************* *********************** S W I T C H ******************************* *************************************************************************/
def expand(x): yield x while x.payload: x = x.payload yield x
def handle_pkt(pkt): if ProbeData in pkt: data_layers = [l for l in expand(pkt) if l.name=='ProbeData'] print "" for sw in data_layers: utilization = 0 if sw.cur_time == sw.last_time else 8.0*sw.byte_cnt/(sw.cur_time - sw.last_time) print "Switch {} - Port {}: {} Mbps".format(sw.swid, sw.port, utilization)