首页 > 怎么修改下面的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)
【热门文章】
【热门文章】