zoukankan      html  css  js  c++  java
  • Mininet系列实验(六):Mininet动态改变转发规则实验

    一、 实验目的

    熟悉Mininet自定义拓扑脚本的编写;
    熟悉编写POX脚本动态改变转发规则

    二、实验原理

    在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为。在本实验中,基于Mininet脚本,编写POX脚本,动态改变交换机的转发规则。

    三、实验任务

    先看一下本次实验的拓扑图:

    基于Mininet使用POX动态改变转发规则实验 图1

    在该环境下,假设H1 ping H4,初始的路由规则是S1-S2-S5,一秒后,路由转发规则变为S1-S3-S5,再过一秒,规则变为S1-S4-S5,然后再回到最初的转发规则S1-S2-S5。通过这个循环调度的例子动态地改变交换机的转发规则。

    1.1在pox文件里面创建脚本lab_controller.py

      1 from pox.core import core
      2  
      3 import pox.openflow.libopenflow_01 as of
      4  
      5 from pox.lib.util import dpidToStr
      6  
      7 from pox.lib.addresses import IPAddr, EthAddr
      8  
      9 from pox.lib.packet.arp import arp
     10  
     11 from pox.lib.packet.ethernet import ethernet, ETHER_BROADCAST
     12  
     13 from pox.lib.packet.packet_base import packet_base
     14  
     15 from pox.lib.packet.packet_utils import *
     16  
     17 import pox.lib.packet as pkt
     18  
     19 from pox.lib.recoco import Timer
     20  
     21 import time
     22  
     23  
     24  
     25 log = core.getLogger()
     26  
     27  
     28  
     29 s1_dpid=0
     30  
     31 s2_dpid=0
     32  
     33 s3_dpid=0
     34  
     35 s4_dpid=0
     36  
     37 s5_dpid=0
     38  
     39  
     40  
     41 s1_p1=0
     42  
     43 s1_p4=0
     44  
     45 s1_p5=0
     46  
     47 s1_p6=0
     48  
     49 s2_p1=0
     50  
     51 s3_p1=0
     52  
     53 s4_p1=0
     54  
     55  
     56  
     57 pre_s1_p1=0
     58  
     59 pre_s1_p4=0
     60  
     61 pre_s1_p5=0
     62  
     63 pre_s1_p6=0
     64  
     65 pre_s2_p1=0
     66  
     67 pre_s3_p1=0
     68  
     69 pre_s4_p1=0
     70  
     71  
     72  
     73 turn=0
     74  
     75  
     76  
     77 def getTheTime():  #fuction to create a timestamp
     78  
     79   flock = time.localtime()
     80  
     81   then = "[%s-%s-%s" %(str(flock.tm_year),str(flock.tm_mon),str(flock.tm_mday))
     82  
     83  
     84  
     85   if int(flock.tm_hour)<10:
     86  
     87     hrs = "0%s" % (str(flock.tm_hour))
     88  
     89   else:
     90  
     91     hrs = str(flock.tm_hour)
     92  
     93   if int(flock.tm_min)<10:
     94  
     95     mins = str(flock.tm_min)
     96  
     97     secs = "0%s" % (str(flock.tm_sec))
     98  
     99   else:
    100  
    101     secs = str(flock.tm_sec)
    102  
    103  
    104  
    105   then +="]%s.%s.%s" % (hrs,mins,secs)
    106  
    107   return then
    108  
    109  
    110  
    111  
    112  
    113 def _timer_func ():
    114  
    115   global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid,turn
    116  
    117  
    118  
    119  
    120  
    121   #print getTheTime(), "sent the port stats request to s1_dpid"
    122  
    123  
    124  
    125   if turn==0:
    126  
    127       msg = of.ofp_flow_mod()
    128  
    129       msg.command=of.OFPFC_MODIFY_STRICT
    130  
    131       msg.priority =100
    132  
    133       msg.idle_timeout = 0
    134  
    135       msg.hard_timeout = 0
    136  
    137       msg.match.dl_type = 0x0800
    138  
    139       msg.match.nw_dst = "10.0.0.4"
    140  
    141       msg.actions.append(of.ofp_action_output(port = 5))
    142  
    143       core.openflow.getConnection(s1_dpid).send(msg)
    144  
    145       turn=1
    146  
    147       return
    148  
    149  
    150  
    151   if turn==1:
    152  
    153       msg = of.ofp_flow_mod()
    154  
    155       msg.command=of.OFPFC_MODIFY_STRICT
    156  
    157       msg.priority =100
    158  
    159       msg.idle_timeout = 0
    160  
    161       msg.hard_timeout = 0
    162  
    163       msg.match.dl_type = 0x0800
    164  
    165       msg.match.nw_dst = "10.0.0.4"
    166  
    167       msg.actions.append(of.ofp_action_output(port = 6))
    168  
    169       core.openflow.getConnection(s1_dpid).send(msg)
    170  
    171       turn=2
    172  
    173       return
    174  
    175  
    176  
    177   if turn==2:
    178  
    179       msg = of.ofp_flow_mod()
    180  
    181       msg.command=of.OFPFC_MODIFY_STRICT
    182  
    183       msg.priority =100
    184  
    185       msg.idle_timeout = 0
    186  
    187       msg.hard_timeout = 0
    188  
    189       msg.match.dl_type = 0x0800
    190  
    191       msg.match.nw_dst = "10.0.0.4"
    192  
    193       msg.actions.append(of.ofp_action_output(port = 4))
    194  
    195       turn=0
    196  
    197       return
    198  
    199  
    200  
    201 def _handle_portstats_received (event):
    202  
    203   global s1_p1,s1_p4, s1_p5, s1_p6, s2_p1, s3_p1, s4_p1
    204  
    205   global pre_s1_p1,pre_s1_p4, pre_s1_p5, pre_s1_p6, pre_s2_p1, pre_s3_p1, pre_s4_p1
    206  
    207  
    208  
    209   if event.connection.dpid==s1_dpid:
    210  
    211     for f in event.stats:
    212  
    213       if int(f.port_no)<65534:
    214  
    215         if f.port_no==1:
    216  
    217           pre_s1_p1=s1_p1
    218  
    219           s1_p1=f.rx_packets
    220  
    221  
    222         if f.port_no==4:
    223  
    224           pre_s1_p4=s1_p4
    225  
    226           s1_p4=f.tx_packets
    227  
    228           #s1_p4=f.tx_bytes
    229  
    230  
    231         if f.port_no==5:
    232  
    233           pre_s1_p5=s1_p5
    234  
    235           s1_p5=f.tx_packets
    236  
    237         if f.port_no==6:
    238  
    239           pre_s1_p6=s1_p6
    240  
    241           s1_p6=f.tx_packets
    242  
    243  
    244  
    245     for f in event.stats:
    246  
    247        if int(f.port_no)<65534:
    248  
    249          if f.port_no==1:
    250  
    251            pre_s2_p1=s2_p1
    252  
    253            s2_p1=f.rx_packets
    254  
    255            #s2_p1=f.rx_bytes
    256  
    257  
    258   if event.connection.dpid==s3_dpid:
    259  
    260      for f in event.stats:
    261  
    262        if int(f.port_no)<65534:
    263  
    264          if f.port_no==1:
    265  
    266            pre_s3_p1=s3_p1
    267  
    268            s3_p1=f.rx_packets
    269  
    270  
    271  
    272  
    273   if event.connection.dpid==s4_dpid:
    274  
    275      for f in event.stats:
    276  
    277        if int(f.port_no)<65534:
    278  
    279          if f.port_no==1:
    280  
    281            pre_s4_p1=s4_p1
    282  
    283            s4_p1=f.rx_packets
    284  
    285  
    286  
    287  
    288 def _handle_ConnectionUp (event):
    289  
    290   global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid
    291  
    292   print "ConnectionUp: ",dpidToStr(event.connection.dpid)
    293  
    294  
    295  
    296   #remember the connection dpid for switch
    297  
    298   for m in event.connection.features.ports:
    299  
    300     if m.name == "s1-eth1":
    301  
    302       s1_dpid = event.connection.dpid
    303  
    304       print "s1_dpid=", s1_dpid
    305  
    306     elif m.name == "s2-eth1":
    307  
    308       s2_dpid = event.connection.dpid
    309  
    310       print "s2_dpid=", s2_dpid
    311  
    312     elif m.name == "s3-eth1":
    313  
    314       s3_dpid = event.connection.dpid
    315  
    316     elif m.name == "s4-eth1":
    317  
    318       s4_dpid = event.connection.dpid
    319  
    320       print "s4_dpid=", s4_dpid
    321  
    322     elif m.name == "s5-eth1":
    323  
    324       s5_dpid = event.connection.dpid
    325  
    326       print "s5_dpid=", s5_dpid
    327  
    328  
    329  
    330   if s1_dpid<>0 and s2_dpid<>0 and s3_dpid<>0 and s4_dpid<>0:
    331  
    332     Timer(1, _timer_func, recurring=True)
    333  
    334  
    335  
    336 def _handle_PacketIn(event):
    337  
    338   global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid
    339  
    340  
    341  
    342   packet=event.parsed
    343  
    344  
    345  
    346  
    347   if event.connection.dpid==s1_dpid:
    348  
    349      a=packet.find('arp')
    350  
    351      if a and a.protodst=="10.0.0.4":
    352  
    353        msg = of.ofp_packet_out(data=event.ofp)
    354  
    355        msg.actions.append(of.ofp_action_output(port=4))
    356  
    357        event.connection.send(msg)
    358  
    359  
    360  
    361      if a and a.protodst=="10.0.0.5":
    362  
    363        msg = of.ofp_packet_out(data=event.ofp)
    364  
    365        msg.actions.append(of.ofp_action_output(port=5))
    366  
    367        event.connection.send(msg)
    368  
    369  
    370  
    371      if a and a.protodst=="10.0.0.6":
    372  
    373        msg = of.ofp_packet_out(data=event.ofp)
    374  
    375        msg.actions.append(of.ofp_action_output(port=6))
    376  
    377        event.connection.send(msg)
    378  
    379  
    380  
    381      if a and a.protodst=="10.0.0.1":
    382  
    383        msg = of.ofp_packet_out(data=event.ofp)
    384  
    385        msg.actions.append(of.ofp_action_output(port=1))
    386  
    387        event.connection.send(msg)
    388  
    389  
    390  
    391      if a and a.protodst=="10.0.0.2":
    392  
    393        msg = of.ofp_packet_out(data=event.ofp)
    394  
    395        msg.actions.append(of.ofp_action_output(port=2))
    396  
    397        event.connection.send(msg)
    398  
    399  
    400  
    401      if a and a.protodst=="10.0.0.3":
    402  
    403        msg = of.ofp_packet_out(data=event.ofp)
    404  
    405        msg.actions.append(of.ofp_action_output(port=3))
    406  
    407        event.connection.send(msg)
    408  
    409  
    410  
    411      msg = of.ofp_flow_mod()
    412  
    413      msg.priority =100
    414  
    415      msg.idle_timeout = 0
    416  
    417      msg.hard_timeout = 0
    418  
    419      msg.match.dl_type = 0x0800
    420  
    421      msg.match.nw_dst = "10.0.0.1"
    422  
    423      msg.actions.append(of.ofp_action_output(port = 1))
    424  
    425      event.connection.send(msg)
    426  
    427  
    428  
    429      msg = of.ofp_flow_mod()
    430  
    431      msg.priority =100
    432  
    433      msg.idle_timeout = 0
    434  
    435      msg.hard_timeout = 0
    436  
    437      msg.match.dl_type = 0x0800
    438  
    439      msg.match.nw_dst = "10.0.0.2"
    440  
    441      msg.actions.append(of.ofp_action_output(port = 2))
    442  
    443      event.connection.send(msg)
    444  
    445  
    446  
    447      msg = of.ofp_flow_mod()
    448  
    449      msg.priority =100
    450  
    451      msg.idle_timeout = 0
    452  
    453      msg.hard_timeout = 0
    454  
    455      msg.match.dl_type = 0x0800
    456  
    457      msg.match.nw_dst = "10.0.0.3"
    458  
    459      msg.actions.append(of.ofp_action_output(port = 3))
    460  
    461      event.connection.send(msg)
    462  
    463  
    464  
    465      msg = of.ofp_flow_mod()
    466  
    467      msg.priority =100
    468  
    469      msg.idle_timeout = 0
    470  
    471      msg.hard_timeout = 1
    472  
    473      msg.match.dl_type = 0x0800
    474  
    475      msg.match.nw_dst = "10.0.0.4"
    476  
    477      msg.actions.append(of.ofp_action_output(port = 4))
    478  
    479      event.connection.send(msg)
    480  
    481  
    482  
    483      msg = of.ofp_flow_mod()
    484  
    485      msg.priority =100
    486  
    487      msg.idle_timeout = 0
    488  
    489      msg.hard_timeout = 0
    490  
    491      msg.match.dl_type = 0x0800
    492  
    493      msg.match.nw_dst = "10.0.0.5"
    494  
    495      msg.actions.append(of.ofp_action_output(port = 5))
    496  
    497      event.connection.send(msg)
    498  
    499  
    500  
    501      msg = of.ofp_flow_mod()
    502  
    503      msg.priority =100
    504  
    505      msg.idle_timeout = 0
    506  
    507      msg.hard_timeout = 0
    508  
    509      msg.match.dl_type = 0x0800
    510  
    511      msg.match.nw_dst = "10.0.0.6"
    512  
    513      msg.actions.append(of.ofp_action_output(port = 6))
    514  
    515      event.connection.send(msg)
    516  
    517  
    518  
    519   elif event.connection.dpid==s2_dpid:
    520  
    521      msg = of.ofp_flow_mod()
    522  
    523      msg.priority =10
    524  
    525      msg.idle_timeout = 0
    526  
    527      msg.hard_timeout = 0
    528  
    529      msg.match.in_port = 1
    530  
    531      msg.match.dl_type=0x0806
    532  
    533      msg.actions.append(of.ofp_action_output(port = 2))
    534  
    535      event.connection.send(msg)
    536  
    537  
    538  
    539      msg = of.ofp_flow_mod()
    540  
    541      msg.priority =10
    542  
    543      msg.idle_timeout = 0
    544  
    545      msg.hard_timeout = 0
    546  
    547      msg.match.in_port = 1
    548  
    549      msg.match.dl_type=0x0800
    550  
    551      msg.actions.append(of.ofp_action_output(port = 2))
    552  
    553      event.connection.send(msg)
    554  
    555  
    556  
    557      msg = of.ofp_flow_mod()
    558  
    559      msg.priority =10
    560  
    561      msg.idle_timeout = 0
    562  
    563      msg.hard_timeout = 0
    564  
    565      msg.match.in_port = 2
    566  
    567      msg.match.dl_type=0x0806
    568  
    569      msg.actions.append(of.ofp_action_output(port = 1))
    570  
    571      event.connection.send(msg)
    572  
    573  
    574  
    575      msg = of.ofp_flow_mod()
    576  
    577      msg.priority =10
    578  
    579      msg.idle_timeout = 0
    580  
    581      msg.hard_timeout = 0
    582  
    583      msg.match.in_port = 2
    584  
    585      msg.match.dl_type=0x0800
    586  
    587      msg.actions.append(of.ofp_action_output(port = 1))
    588  
    589      event.connection.send(msg)
    590  
    591  
    592  
    593   elif event.connection.dpid==s3_dpid:
    594  
    595      msg = of.ofp_flow_mod()
    596  
    597      msg.priority =10
    598  
    599      msg.idle_timeout = 0
    600  
    601      msg.hard_timeout = 0
    602  
    603      msg.match.in_port = 1
    604  
    605      msg.match.dl_type=0x0806
    606  
    607      msg.actions.append(of.ofp_action_output(port = 2))
    608  
    609      event.connection.send(msg)
    610  
    611  
    612  
    613      msg = of.ofp_flow_mod()
    614  
    615      msg.priority =10
    616  
    617      msg.idle_timeout = 0
    618  
    619      msg.hard_timeout = 0
    620  
    621      msg.match.in_port = 1
    622  
    623      msg.match.dl_type=0x0800
    624  
    625      msg.actions.append(of.ofp_action_output(port = 2))
    626  
    627      event.connection.send(msg)
    628  
    629  
    630  
    631      msg = of.ofp_flow_mod()
    632  
    633      msg.priority =10
    634  
    635      msg.idle_timeout = 0
    636  
    637      msg.hard_timeout = 0
    638  
    639      msg.match.in_port = 2
    640  
    641      msg.match.dl_type=0x0806
    642  
    643      msg.actions.append(of.ofp_action_output(port = 1))
    644  
    645      event.connection.send(msg)
    646  
    647  
    648  
    649      msg = of.ofp_flow_mod()
    650  
    651      msg.priority =10
    652  
    653      msg.idle_timeout = 0
    654  
    655      msg.hard_timeout = 0
    656  
    657      msg.match.in_port = 2
    658  
    659      msg.match.dl_type=0x0800
    660  
    661      msg.actions.append(of.ofp_action_output(port = 1))
    662  
    663      event.connection.send(msg)
    664  
    665  
    666  
    667   elif event.connection.dpid==s4_dpid:
    668  
    669      msg = of.ofp_flow_mod()
    670  
    671      msg.priority =10
    672  
    673      msg.idle_timeout = 0
    674  
    675      msg.hard_timeout = 0
    676  
    677      msg.match.in_port = 1
    678  
    679      msg.match.dl_type=0x0806
    680  
    681      msg.actions.append(of.ofp_action_output(port = 2))
    682  
    683      event.connection.send(msg)
    684  
    685  
    686  
    687      msg = of.ofp_flow_mod()
    688  
    689      msg.priority =10
    690  
    691      msg.idle_timeout = 0
    692  
    693      msg.hard_timeout = 0
    694  
    695      msg.match.in_port = 1
    696  
    697      msg.match.dl_type=0x0800
    698  
    699      msg.actions.append(of.ofp_action_output(port = 2))
    700  
    701      event.connection.send(msg)
    702  
    703  
    704  
    705      msg = of.ofp_flow_mod()
    706  
    707      msg.priority =10
    708  
    709      msg.idle_timeout = 0
    710  
    711      msg.hard_timeout = 0
    712  
    713      msg.match.in_port = 2
    714  
    715      msg.match.dl_type=0x0806
    716  
    717      msg.actions.append(of.ofp_action_output(port = 1))
    718  
    719      event.connection.send(msg)
    720  
    721  
    722  
    723      msg = of.ofp_flow_mod()
    724  
    725      msg.priority =10
    726  
    727      msg.idle_timeout = 0
    728  
    729      msg.hard_timeout = 0
    730  
    731      msg.match.in_port = 2
    732  
    733      msg.match.dl_type=0x0800
    734  
    735      msg.actions.append(of.ofp_action_output(port = 1))
    736  
    737      event.connection.send(msg)
    738  
    739  
    740  
    741   elif event.connection.dpid==s5_dpid:
    742  
    743      a=packet.find('arp')
    744  
    745      if a and a.protodst=="10.0.0.4":
    746  
    747        msg = of.ofp_packet_out(data=event.ofp)
    748  
    749        msg.actions.append(of.ofp_action_output(port=4))
    750  
    751        event.connection.send(msg)
    752  
    753  
    754  
    755      if a and a.protodst=="10.0.0.5":
    756  
    757        msg = of.ofp_packet_out(data=event.ofp)
    758  
    759        msg.actions.append(of.ofp_action_output(port=5))
    760  
    761        event.connection.send(msg)
    762  
    763  
    764  
    765      if a and a.protodst=="10.0.0.6":
    766  
    767        msg = of.ofp_packet_out(data=event.ofp)
    768  
    769        msg.actions.append(of.ofp_action_output(port=6))
    770  
    771        event.connection.send(msg)
    772  
    773  
    774  
    775      if a and a.protodst=="10.0.0.1":
    776  
    777        msg = of.ofp_packet_out(data=event.ofp)
    778  
    779        msg.actions.append(of.ofp_action_output(port=1))
    780  
    781        event.connection.send(msg)
    782  
    783  
    784  
    785      if a and a.protodst=="10.0.0.2":
    786  
    787        msg = of.ofp_packet_out(data=event.ofp)
    788  
    789        msg.actions.append(of.ofp_action_output(port=2))
    790  
    791        event.connection.send(msg)
    792  
    793  
    794  
    795      if a and a.protodst=="10.0.0.3":
    796  
    797        msg = of.ofp_packet_out(data=event.ofp)
    798  
    799        msg.actions.append(of.ofp_action_output(port=3))
    800  
    801        event.connection.send(msg)
    802  
    803  
    804  
    805  
    806  
    807  
    808      msg = of.ofp_flow_mod()
    809  
    810      msg.priority =100
    811  
    812      msg.idle_timeout = 0
    813  
    814      msg.hard_timeout = 0
    815  
    816      msg.match.dl_type = 0x0800
    817  
    818      msg.match.nw_dst = "10.0.0.1"
    819  
    820      msg.actions.append(of.ofp_action_output(port = 1))
    821  
    822      event.connection.send(msg)
    823  
    824  
    825  
    826      msg = of.ofp_flow_mod()
    827  
    828      msg.priority =100
    829  
    830      msg.idle_timeout = 0
    831  
    832      msg.hard_timeout = 0
    833  
    834      msg.match.dl_type = 0x0800
    835  
    836      msg.match.nw_dst = "10.0.0.2"
    837  
    838      msg.actions.append(of.ofp_action_output(port = 2))
    839  
    840      event.connection.send(msg)
    841  
    842  
    843  
    844      msg = of.ofp_flow_mod()
    845  
    846      msg.priority =100
    847  
    848      msg.idle_timeout = 0
    849  
    850      msg.hard_timeout = 0
    851  
    852      msg.match.dl_type = 0x0800
    853  
    854      msg.match.nw_dst = "10.0.0.3"
    855  
    856      msg.actions.append(of.ofp_action_output(port = 3))
    857  
    858      event.connection.send(msg)
    859  
    860  
    861  
    862      msg = of.ofp_flow_mod()
    863  
    864      msg.priority =100
    865  
    866      msg.idle_timeout = 0
    867  
    868      msg.hard_timeout = 0
    869  
    870      msg.match.dl_type = 0x0800
    871  
    872      msg.match.nw_dst = "10.0.0.4"
    873  
    874      msg.actions.append(of.ofp_action_output(port = 4))
    875  
    876      event.connection.send(msg)
    877  
    878  
    879  
    880      msg = of.ofp_flow_mod()
    881  
    882      msg.priority =100
    883  
    884      msg.idle_timeout = 0
    885  
    886      msg.hard_timeout = 0
    887  
    888      msg.match.dl_type = 0x0800
    889  
    890      msg.match.nw_dst = "10.0.0.5"
    891  
    892      msg.actions.append(of.ofp_action_output(port = 5))
    893  
    894      event.connection.send(msg)
    895  
    896  
    897  
    898      msg = of.ofp_flow_mod()
    899  
    900      msg.priority =100
    901  
    902      msg.idle_timeout = 0
    903  
    904      msg.hard_timeout = 0
    905  
    906      msg.match.dl_type = 0x0800
    907  
    908      msg.match.nw_dst = "10.0.0.6"
    909  
    910      msg.actions.append(of.ofp_action_output(port = 6))
    911  
    912      event.connection.send(msg)
    913  
    914  
    915  
    916 def launch ():
    917  
    918   global start_time
    919  
    920   core.openflow.addListenerByName("PortStatsReceived",_handle_portstats_received)
    921  
    922   core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)
    923  
    924   core.openflow.addListenerByName("PacketIn",_handle_PacketIn)

    1.2查看流表项(做完步骤2再回来)

    1 ./pox.py lab_controller web messenger messenger.log_service messenger.ajax_transport openflow.of_service poxdesk

    然后再火狐里面搜索:http://pox-ip:8000/poxdesk然后点击TableViewer选择s1即00-00-00-00-01就会跳出下图(其中深蓝色的交换机的output的端口就是在5,6的切换,时而还会出现不存在(个人是这认为ox脚本的bug)说明了只有两种切换方式,而另一种(连向4的)不通,说明了网络运行的时候在s1的OUTPUT:4处出现了问题,这个待我日后分析lab_controller.py再说吧。

    2.1在mininet文件夹里面创建脚本文件rulemininet.py

      1 #!/usr/bin/python
      2  
      3 # coding:utf -8 
      4  
      5 from mininet.topo import Topo
      6  
      7 from mininet.net import Mininet
      8  
      9 from mininet.node import CPULimitedHost
     10  
     11 from mininet.link import TCLink
     12  
     13 from mininet.util import dumpNodeConnections
     14  
     15 from mininet.log import setLogLevel
     16  
     17 from mininet.node import Controller 
     18  
     19 from mininet.cli import CLI
     20  
     21 from functools import partial
     22  
     23 from mininet.node import RemoteController
     24  
     25 import os
     26  
     27  
     28  # 创建一个基于拓扑类的MyTopo类使得多交换机连接多个主机和交换机的复杂拓扑
     29  
     30 class MyTopo(Topo):
     31  
     32     "Single switch connected to n hosts."
     33     # 构造函数创建5个交换机和6个主机
     34  
     35     def __init__(self):
     36  
     37         Topo.__init__(self)
     38  
     39         s1=self.addSwitch('s1')
     40  
     41         s2=self.addSwitch('s2')
     42  
     43         s3=self.addSwitch('s3')
     44  
     45         s4=self.addSwitch('s4')
     46  
     47         s5=self.addSwitch('s5') 
     48  
     49         h1=self.addHost('h1')
     50  
     51         h2=self.addHost('h2')
     52  
     53         h3=self.addHost('h3')
     54  
     55         h4=self.addHost('h4')
     56  
     57         h5=self.addHost('h5')
     58  
     59         h6=self.addHost('h6')
     60  
     61         # 主机1,2,3连接s1
     62  
     63         self.addLink(h1, s1, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
     64  
     65         self.addLink(h2, s1, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     66  
     67         self.addLink(h3, s1, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
     68  
     69         # 交换机2,3,4连接s1,所以说s1有6个接口
     70  
     71         self.addLink(s1, s2, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     72  
     73         self.addLink(s1, s3, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     74  
     75         self.addLink(s1, s4, bw=1, delay='10ms', loss=0, max_queue_size=1000,use_htb=True)
     76  
     77           # 交换机2,3,4连接s5,所以说s5也有6个接口,2,3,4各两个
     78  
     79         self.addLink(s2, s5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     80  
     81         self.addLink(s3, s5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)  
     82  
     83         self.addLink(s4, s5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     84  
     85         # 主机4,5,6连接s5
     86  
     87         self.addLink(s5, h4, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     88  
     89         self.addLink(s5, h5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     90  
     91         self.addLink(s5, h6, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True) 
     92  
     93  
     94  
     95 def perfTest():
     96  
     97     # 创建网络并运行简单性能测试
     98  
     99     topo = MyTopo()
    100  
    101  
    102     net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink, controller=partial(RemoteController, ip='10.0.0.13', port=6633))
    103  
    104     net.start()
    105  
    106     print "Dumping host connections"
    107  
    108     dumpNodeConnections(net.hosts)
    109  
    110     h1,h2,h3=net.get('h1','h2','h3')
    111  
    112     h4,h5,h6=net.get('h4','h5','h6')
    113  
    114     h1.setMAC("0:0:0:0:0:1")
    115  
    116     h2.setMAC("0:0:0:0:0:2")
    117  
    118     h3.setMAC("0:0:0:0:0:3")
    119  
    120     h4.setMAC("0:0:0:0:0:4")
    121  
    122     h5.setMAC("0:0:0:0:0:5")
    123  
    124     h6.setMAC("0:0:0:0:0:6")
    125  
    126     CLI(net)
    127  
    128     net.stop()
    129  
    130  
    131  
    132 if __name__ == '__main__':
    133  
    134     setLogLevel('info')
    135  
    136     perfTest()

    2.2 先打开脚本文件lab_controller.py然后打开rulemininet.py ,分别用两个程序

     1  
     2 cd pox
     3  
     4 ./pox.py lab_controller
     5  
     6 cd mininet
     7  
     8 ./rulemininet.py
     9  
    10 h1 ping -i 0.1 h4

    rulemininet.py脚本的显示情况:

    然而,pox理论上应当显示的是:

    基于Mininet使用POX动态改变转发规则实验 图12

    而我的是。。

    所以这应当是pox的脚本问题,于是我才使用查看流表项的方式来分析。

    总结:

    1.学会了使用查看流表项

    2.理解这个动态拓扑的原理

  • 相关阅读:
    洛谷 P2872 [USACO07DEC]道路建设Building Roads
    cogs 29. 公路建设
    cogs 7. 通信线路
    cogs 2478. [HZOI 2016]简单的最近公共祖先
    洛谷 P1342 请柬
    洛谷 P1186 玛丽卡
    洛谷 P1491 集合位置
    启动、停止、重启服务
    洛谷——P1025 数的划分
    洛谷——P3368 【模板】树状数组 2
  • 原文地址:https://www.cnblogs.com/a1164520408/p/9629192.html
Copyright © 2011-2022 走看看