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

发布于 2022-09-01 18:44:34 字数 3009 浏览 17 评论 0

这是具体的题目要求
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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文