python求奇偶性_怎么修改下面的Python代码来实现奇偶性的通信?

这是具体的题目要求

modify the application in order to implement a simple port-based VLAN. Separate the traffic between even and odd numbered ports - e.g. odd ports can only communicate with other odd ports, and even ports can only communicate with other even ports. However, you will have to take into consideration the following rules:

• Ports 1 and 2 should belong to both VLANs. This means that the hosts connected to port 1 and 2 should be able to ping all other hosts connected to the switch.

• Special attention should be paid to broadcast messages. When a host on the virtual network issues a broadcast, the message should only be seen by those who are a part of its VLAN. Consider a case where the OVS has 6 hosts connected; then the view of each VLAN would be as shown below in Figure 2.

• Note that 'actions' variable in the packet handling function is a 'list' data structure. Thus, it’s possible to associate multiple actions (i.e. output to multiple ports) with just a single rule.

• If the ‘actions’ list is empty (i.e. []), then that corresponds to an action to drop any packets that matches the flow.

这是具体代码:

def handle_packet_in(self, ev):

msg = ev.msg

datapath = msg.datapath

ofproto = datapath.ofproto

dst, src, _eth_type = struct.unpack_from('!6s6sH', buffer(msg.data), 0)

dpid = datapath.id #Switch ID

self.mac2port.setdefault(dpid, {}) #Create new record for this switch

#If it didn't exist before

LOG.info("Src MAC: %s; Dest MAC: %s", haddr_to_str(src), haddr_to_str(dst))

self.mac2port[dpid][src] = msg.in_port #Save record of port number

# associated with MAC address

broadcast = (dst == mac.BROADCAST) or mac.is_multicast(dst)

if broadcast:

out_port = ofproto.OFPP_FLOOD

LOG.info("broadcast frame,flood and install flow")

else:

if src != dst:

# Fetch port number associated with destination MAC

# Return None if no record exists

out_port = self.mac2port[dpid].get(dst, None)

if out_port == None:

LOG.info("Output port not found")

out_port = ofproto.OFPP_FLOOD

else:

self._drop_packet(msg)

return

actions = [datapath.ofproto_parser.OFPActionOutput (out_port)]

LOG.info("Input port: %s; Output port: %s", msg.in_port, out_port)

rule = nx_match.ClsRule()

rule.set_in_port(msg.in_port)

rule.set_dl_dst(dst)

rule.set_dl_src(src)

rule.set_nw_dscp(0)

datapath.send_flow_mod(

rule=rule, cookie=0, commend=ofproto.OFPFC_ADD,

idle_timeout=60, hard_timeout=60,

priority=ofproto.OFP_DEFAULT_PRIORITY,

flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)

datapath.send_packet_out(msg.buffer_id, msg.in_port, actions)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值