一、 实验目的

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

二、实验原理

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

三、实验任务

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

在该环境下,假设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. log = core.getLogger()
  24.  
  25. s1_dpid=0
  26.  
  27. s2_dpid=0
  28.  
  29. s3_dpid=0
  30.  
  31. s4_dpid=0
  32.  
  33. s5_dpid=0
  34.  
  35. s1_p1=0
  36.  
  37. s1_p4=0
  38.  
  39. s1_p5=0
  40.  
  41. s1_p6=0
  42.  
  43. s2_p1=0
  44.  
  45. s3_p1=0
  46.  
  47. s4_p1=0
  48.  
  49. pre_s1_p1=0
  50.  
  51. pre_s1_p4=0
  52.  
  53. pre_s1_p5=0
  54.  
  55. pre_s1_p6=0
  56.  
  57. pre_s2_p1=0
  58.  
  59. pre_s3_p1=0
  60.  
  61. pre_s4_p1=0
  62.  
  63. turn=0
  64.  
  65. def getTheTime(): #fuction to create a timestamp
  66.  
  67. flock = time.localtime()
  68.  
  69. then = "[%s-%s-%s" %(str(flock.tm_year),str(flock.tm_mon),str(flock.tm_mday))
  70.  
  71. if int(flock.tm_hour)<10:
  72.  
  73. hrs = "0%s" % (str(flock.tm_hour))
  74.  
  75. else:
  76.  
  77. hrs = str(flock.tm_hour)
  78.  
  79. if int(flock.tm_min)<10:
  80.  
  81. mins = str(flock.tm_min)
  82.  
  83. secs = "0%s" % (str(flock.tm_sec))
  84.  
  85. else:
  86.  
  87. secs = str(flock.tm_sec)
  88.  
  89. then +="]%s.%s.%s" % (hrs,mins,secs)
  90.  
  91. return then
  92.  
  93. def _timer_func ():
  94.  
  95. global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid,turn
  96.  
  97. #print getTheTime(), "sent the port stats request to s1_dpid"
  98.  
  99. if turn==0:
  100.  
  101. msg = of.ofp_flow_mod()
  102.  
  103. msg.command=of.OFPFC_MODIFY_STRICT
  104.  
  105. msg.priority =100
  106.  
  107. msg.idle_timeout = 0
  108.  
  109. msg.hard_timeout = 0
  110.  
  111. msg.match.dl_type = 0x0800
  112.  
  113. msg.match.nw_dst = "10.0.0.4"
  114.  
  115. msg.actions.append(of.ofp_action_output(port = 5))
  116.  
  117. core.openflow.getConnection(s1_dpid).send(msg)
  118.  
  119. turn=1
  120.  
  121. return
  122.  
  123. if turn==1:
  124.  
  125. msg = of.ofp_flow_mod()
  126.  
  127. msg.command=of.OFPFC_MODIFY_STRICT
  128.  
  129. msg.priority =100
  130.  
  131. msg.idle_timeout = 0
  132.  
  133. msg.hard_timeout = 0
  134.  
  135. msg.match.dl_type = 0x0800
  136.  
  137. msg.match.nw_dst = "10.0.0.4"
  138.  
  139. msg.actions.append(of.ofp_action_output(port = 6))
  140.  
  141. core.openflow.getConnection(s1_dpid).send(msg)
  142.  
  143. turn=2
  144.  
  145. return
  146.  
  147. if turn==2:
  148.  
  149. msg = of.ofp_flow_mod()
  150.  
  151. msg.command=of.OFPFC_MODIFY_STRICT
  152.  
  153. msg.priority =100
  154.  
  155. msg.idle_timeout = 0
  156.  
  157. msg.hard_timeout = 0
  158.  
  159. msg.match.dl_type = 0x0800
  160.  
  161. msg.match.nw_dst = "10.0.0.4"
  162.  
  163. msg.actions.append(of.ofp_action_output(port = 4))
  164.  
  165. turn=0
  166.  
  167. return
  168.  
  169. def _handle_portstats_received (event):
  170.  
  171. global s1_p1,s1_p4, s1_p5, s1_p6, s2_p1, s3_p1, s4_p1
  172.  
  173. global pre_s1_p1,pre_s1_p4, pre_s1_p5, pre_s1_p6, pre_s2_p1, pre_s3_p1, pre_s4_p1
  174.  
  175. if event.connection.dpid==s1_dpid:
  176.  
  177. for f in event.stats:
  178.  
  179. if int(f.port_no)<65534:
  180.  
  181. if f.port_no==1:
  182.  
  183. pre_s1_p1=s1_p1
  184.  
  185. s1_p1=f.rx_packets
  186.  
  187. if f.port_no==4:
  188.  
  189. pre_s1_p4=s1_p4
  190.  
  191. s1_p4=f.tx_packets
  192.  
  193. #s1_p4=f.tx_bytes
  194.  
  195. if f.port_no==5:
  196.  
  197. pre_s1_p5=s1_p5
  198.  
  199. s1_p5=f.tx_packets
  200.  
  201. if f.port_no==6:
  202.  
  203. pre_s1_p6=s1_p6
  204.  
  205. s1_p6=f.tx_packets
  206.  
  207. for f in event.stats:
  208.  
  209. if int(f.port_no)<65534:
  210.  
  211. if f.port_no==1:
  212.  
  213. pre_s2_p1=s2_p1
  214.  
  215. s2_p1=f.rx_packets
  216.  
  217. #s2_p1=f.rx_bytes
  218.  
  219. if event.connection.dpid==s3_dpid:
  220.  
  221. for f in event.stats:
  222.  
  223. if int(f.port_no)<65534:
  224.  
  225. if f.port_no==1:
  226.  
  227. pre_s3_p1=s3_p1
  228.  
  229. s3_p1=f.rx_packets
  230.  
  231. if event.connection.dpid==s4_dpid:
  232.  
  233. for f in event.stats:
  234.  
  235. if int(f.port_no)<65534:
  236.  
  237. if f.port_no==1:
  238.  
  239. pre_s4_p1=s4_p1
  240.  
  241. s4_p1=f.rx_packets
  242.  
  243. def _handle_ConnectionUp (event):
  244.  
  245. global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid
  246.  
  247. print "ConnectionUp: ",dpidToStr(event.connection.dpid)
  248.  
  249. #remember the connection dpid for switch
  250.  
  251. for m in event.connection.features.ports:
  252.  
  253. if m.name == "s1-eth1":
  254.  
  255. s1_dpid = event.connection.dpid
  256.  
  257. print "s1_dpid=", s1_dpid
  258.  
  259. elif m.name == "s2-eth1":
  260.  
  261. s2_dpid = event.connection.dpid
  262.  
  263. print "s2_dpid=", s2_dpid
  264.  
  265. elif m.name == "s3-eth1":
  266.  
  267. s3_dpid = event.connection.dpid
  268.  
  269. elif m.name == "s4-eth1":
  270.  
  271. s4_dpid = event.connection.dpid
  272.  
  273. print "s4_dpid=", s4_dpid
  274.  
  275. elif m.name == "s5-eth1":
  276.  
  277. s5_dpid = event.connection.dpid
  278.  
  279. print "s5_dpid=", s5_dpid
  280.  
  281. if s1_dpid<>0 and s2_dpid<>0 and s3_dpid<>0 and s4_dpid<>0:
  282.  
  283. Timer(1, _timer_func, recurring=True)
  284.  
  285. def _handle_PacketIn(event):
  286.  
  287. global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid
  288.  
  289. packet=event.parsed
  290.  
  291. if event.connection.dpid==s1_dpid:
  292.  
  293. a=packet.find('arp')
  294.  
  295. if a and a.protodst=="10.0.0.4":
  296.  
  297. msg = of.ofp_packet_out(data=event.ofp)
  298.  
  299. msg.actions.append(of.ofp_action_output(port=4))
  300.  
  301. event.connection.send(msg)
  302.  
  303. if a and a.protodst=="10.0.0.5":
  304.  
  305. msg = of.ofp_packet_out(data=event.ofp)
  306.  
  307. msg.actions.append(of.ofp_action_output(port=5))
  308.  
  309. event.connection.send(msg)
  310.  
  311. if a and a.protodst=="10.0.0.6":
  312.  
  313. msg = of.ofp_packet_out(data=event.ofp)
  314.  
  315. msg.actions.append(of.ofp_action_output(port=6))
  316.  
  317. event.connection.send(msg)
  318.  
  319. if a and a.protodst=="10.0.0.1":
  320.  
  321. msg = of.ofp_packet_out(data=event.ofp)
  322.  
  323. msg.actions.append(of.ofp_action_output(port=1))
  324.  
  325. event.connection.send(msg)
  326.  
  327. if a and a.protodst=="10.0.0.2":
  328.  
  329. msg = of.ofp_packet_out(data=event.ofp)
  330.  
  331. msg.actions.append(of.ofp_action_output(port=2))
  332.  
  333. event.connection.send(msg)
  334.  
  335. if a and a.protodst=="10.0.0.3":
  336.  
  337. msg = of.ofp_packet_out(data=event.ofp)
  338.  
  339. msg.actions.append(of.ofp_action_output(port=3))
  340.  
  341. event.connection.send(msg)
  342.  
  343. msg = of.ofp_flow_mod()
  344.  
  345. msg.priority =100
  346.  
  347. msg.idle_timeout = 0
  348.  
  349. msg.hard_timeout = 0
  350.  
  351. msg.match.dl_type = 0x0800
  352.  
  353. msg.match.nw_dst = "10.0.0.1"
  354.  
  355. msg.actions.append(of.ofp_action_output(port = 1))
  356.  
  357. event.connection.send(msg)
  358.  
  359. msg = of.ofp_flow_mod()
  360.  
  361. msg.priority =100
  362.  
  363. msg.idle_timeout = 0
  364.  
  365. msg.hard_timeout = 0
  366.  
  367. msg.match.dl_type = 0x0800
  368.  
  369. msg.match.nw_dst = "10.0.0.2"
  370.  
  371. msg.actions.append(of.ofp_action_output(port = 2))
  372.  
  373. event.connection.send(msg)
  374.  
  375. msg = of.ofp_flow_mod()
  376.  
  377. msg.priority =100
  378.  
  379. msg.idle_timeout = 0
  380.  
  381. msg.hard_timeout = 0
  382.  
  383. msg.match.dl_type = 0x0800
  384.  
  385. msg.match.nw_dst = "10.0.0.3"
  386.  
  387. msg.actions.append(of.ofp_action_output(port = 3))
  388.  
  389. event.connection.send(msg)
  390.  
  391. msg = of.ofp_flow_mod()
  392.  
  393. msg.priority =100
  394.  
  395. msg.idle_timeout = 0
  396.  
  397. msg.hard_timeout = 1
  398.  
  399. msg.match.dl_type = 0x0800
  400.  
  401. msg.match.nw_dst = "10.0.0.4"
  402.  
  403. msg.actions.append(of.ofp_action_output(port = 4))
  404.  
  405. event.connection.send(msg)
  406.  
  407. msg = of.ofp_flow_mod()
  408.  
  409. msg.priority =100
  410.  
  411. msg.idle_timeout = 0
  412.  
  413. msg.hard_timeout = 0
  414.  
  415. msg.match.dl_type = 0x0800
  416.  
  417. msg.match.nw_dst = "10.0.0.5"
  418.  
  419. msg.actions.append(of.ofp_action_output(port = 5))
  420.  
  421. event.connection.send(msg)
  422.  
  423. msg = of.ofp_flow_mod()
  424.  
  425. msg.priority =100
  426.  
  427. msg.idle_timeout = 0
  428.  
  429. msg.hard_timeout = 0
  430.  
  431. msg.match.dl_type = 0x0800
  432.  
  433. msg.match.nw_dst = "10.0.0.6"
  434.  
  435. msg.actions.append(of.ofp_action_output(port = 6))
  436.  
  437. event.connection.send(msg)
  438.  
  439. elif event.connection.dpid==s2_dpid:
  440.  
  441. msg = of.ofp_flow_mod()
  442.  
  443. msg.priority =10
  444.  
  445. msg.idle_timeout = 0
  446.  
  447. msg.hard_timeout = 0
  448.  
  449. msg.match.in_port = 1
  450.  
  451. msg.match.dl_type=0x0806
  452.  
  453. msg.actions.append(of.ofp_action_output(port = 2))
  454.  
  455. event.connection.send(msg)
  456.  
  457. msg = of.ofp_flow_mod()
  458.  
  459. msg.priority =10
  460.  
  461. msg.idle_timeout = 0
  462.  
  463. msg.hard_timeout = 0
  464.  
  465. msg.match.in_port = 1
  466.  
  467. msg.match.dl_type=0x0800
  468.  
  469. msg.actions.append(of.ofp_action_output(port = 2))
  470.  
  471. event.connection.send(msg)
  472.  
  473. msg = of.ofp_flow_mod()
  474.  
  475. msg.priority =10
  476.  
  477. msg.idle_timeout = 0
  478.  
  479. msg.hard_timeout = 0
  480.  
  481. msg.match.in_port = 2
  482.  
  483. msg.match.dl_type=0x0806
  484.  
  485. msg.actions.append(of.ofp_action_output(port = 1))
  486.  
  487. event.connection.send(msg)
  488.  
  489. msg = of.ofp_flow_mod()
  490.  
  491. msg.priority =10
  492.  
  493. msg.idle_timeout = 0
  494.  
  495. msg.hard_timeout = 0
  496.  
  497. msg.match.in_port = 2
  498.  
  499. msg.match.dl_type=0x0800
  500.  
  501. msg.actions.append(of.ofp_action_output(port = 1))
  502.  
  503. event.connection.send(msg)
  504.  
  505. elif event.connection.dpid==s3_dpid:
  506.  
  507. msg = of.ofp_flow_mod()
  508.  
  509. msg.priority =10
  510.  
  511. msg.idle_timeout = 0
  512.  
  513. msg.hard_timeout = 0
  514.  
  515. msg.match.in_port = 1
  516.  
  517. msg.match.dl_type=0x0806
  518.  
  519. msg.actions.append(of.ofp_action_output(port = 2))
  520.  
  521. event.connection.send(msg)
  522.  
  523. msg = of.ofp_flow_mod()
  524.  
  525. msg.priority =10
  526.  
  527. msg.idle_timeout = 0
  528.  
  529. msg.hard_timeout = 0
  530.  
  531. msg.match.in_port = 1
  532.  
  533. msg.match.dl_type=0x0800
  534.  
  535. msg.actions.append(of.ofp_action_output(port = 2))
  536.  
  537. event.connection.send(msg)
  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 = 2
  548.  
  549. msg.match.dl_type=0x0806
  550.  
  551. msg.actions.append(of.ofp_action_output(port = 1))
  552.  
  553. event.connection.send(msg)
  554.  
  555. msg = of.ofp_flow_mod()
  556.  
  557. msg.priority =10
  558.  
  559. msg.idle_timeout = 0
  560.  
  561. msg.hard_timeout = 0
  562.  
  563. msg.match.in_port = 2
  564.  
  565. msg.match.dl_type=0x0800
  566.  
  567. msg.actions.append(of.ofp_action_output(port = 1))
  568.  
  569. event.connection.send(msg)
  570.  
  571. elif event.connection.dpid==s4_dpid:
  572.  
  573. msg = of.ofp_flow_mod()
  574.  
  575. msg.priority =10
  576.  
  577. msg.idle_timeout = 0
  578.  
  579. msg.hard_timeout = 0
  580.  
  581. msg.match.in_port = 1
  582.  
  583. msg.match.dl_type=0x0806
  584.  
  585. msg.actions.append(of.ofp_action_output(port = 2))
  586.  
  587. event.connection.send(msg)
  588.  
  589. msg = of.ofp_flow_mod()
  590.  
  591. msg.priority =10
  592.  
  593. msg.idle_timeout = 0
  594.  
  595. msg.hard_timeout = 0
  596.  
  597. msg.match.in_port = 1
  598.  
  599. msg.match.dl_type=0x0800
  600.  
  601. msg.actions.append(of.ofp_action_output(port = 2))
  602.  
  603. event.connection.send(msg)
  604.  
  605. msg = of.ofp_flow_mod()
  606.  
  607. msg.priority =10
  608.  
  609. msg.idle_timeout = 0
  610.  
  611. msg.hard_timeout = 0
  612.  
  613. msg.match.in_port = 2
  614.  
  615. msg.match.dl_type=0x0806
  616.  
  617. msg.actions.append(of.ofp_action_output(port = 1))
  618.  
  619. event.connection.send(msg)
  620.  
  621. msg = of.ofp_flow_mod()
  622.  
  623. msg.priority =10
  624.  
  625. msg.idle_timeout = 0
  626.  
  627. msg.hard_timeout = 0
  628.  
  629. msg.match.in_port = 2
  630.  
  631. msg.match.dl_type=0x0800
  632.  
  633. msg.actions.append(of.ofp_action_output(port = 1))
  634.  
  635. event.connection.send(msg)
  636.  
  637. elif event.connection.dpid==s5_dpid:
  638.  
  639. a=packet.find('arp')
  640.  
  641. if a and a.protodst=="10.0.0.4":
  642.  
  643. msg = of.ofp_packet_out(data=event.ofp)
  644.  
  645. msg.actions.append(of.ofp_action_output(port=4))
  646.  
  647. event.connection.send(msg)
  648.  
  649. if a and a.protodst=="10.0.0.5":
  650.  
  651. msg = of.ofp_packet_out(data=event.ofp)
  652.  
  653. msg.actions.append(of.ofp_action_output(port=5))
  654.  
  655. event.connection.send(msg)
  656.  
  657. if a and a.protodst=="10.0.0.6":
  658.  
  659. msg = of.ofp_packet_out(data=event.ofp)
  660.  
  661. msg.actions.append(of.ofp_action_output(port=6))
  662.  
  663. event.connection.send(msg)
  664.  
  665. if a and a.protodst=="10.0.0.1":
  666.  
  667. msg = of.ofp_packet_out(data=event.ofp)
  668.  
  669. msg.actions.append(of.ofp_action_output(port=1))
  670.  
  671. event.connection.send(msg)
  672.  
  673. if a and a.protodst=="10.0.0.2":
  674.  
  675. msg = of.ofp_packet_out(data=event.ofp)
  676.  
  677. msg.actions.append(of.ofp_action_output(port=2))
  678.  
  679. event.connection.send(msg)
  680.  
  681. if a and a.protodst=="10.0.0.3":
  682.  
  683. msg = of.ofp_packet_out(data=event.ofp)
  684.  
  685. msg.actions.append(of.ofp_action_output(port=3))
  686.  
  687. event.connection.send(msg)
  688.  
  689. msg = of.ofp_flow_mod()
  690.  
  691. msg.priority =100
  692.  
  693. msg.idle_timeout = 0
  694.  
  695. msg.hard_timeout = 0
  696.  
  697. msg.match.dl_type = 0x0800
  698.  
  699. msg.match.nw_dst = "10.0.0.1"
  700.  
  701. msg.actions.append(of.ofp_action_output(port = 1))
  702.  
  703. event.connection.send(msg)
  704.  
  705. msg = of.ofp_flow_mod()
  706.  
  707. msg.priority =100
  708.  
  709. msg.idle_timeout = 0
  710.  
  711. msg.hard_timeout = 0
  712.  
  713. msg.match.dl_type = 0x0800
  714.  
  715. msg.match.nw_dst = "10.0.0.2"
  716.  
  717. msg.actions.append(of.ofp_action_output(port = 2))
  718.  
  719. event.connection.send(msg)
  720.  
  721. msg = of.ofp_flow_mod()
  722.  
  723. msg.priority =100
  724.  
  725. msg.idle_timeout = 0
  726.  
  727. msg.hard_timeout = 0
  728.  
  729. msg.match.dl_type = 0x0800
  730.  
  731. msg.match.nw_dst = "10.0.0.3"
  732.  
  733. msg.actions.append(of.ofp_action_output(port = 3))
  734.  
  735. event.connection.send(msg)
  736.  
  737. msg = of.ofp_flow_mod()
  738.  
  739. msg.priority =100
  740.  
  741. msg.idle_timeout = 0
  742.  
  743. msg.hard_timeout = 0
  744.  
  745. msg.match.dl_type = 0x0800
  746.  
  747. msg.match.nw_dst = "10.0.0.4"
  748.  
  749. msg.actions.append(of.ofp_action_output(port = 4))
  750.  
  751. event.connection.send(msg)
  752.  
  753. msg = of.ofp_flow_mod()
  754.  
  755. msg.priority =100
  756.  
  757. msg.idle_timeout = 0
  758.  
  759. msg.hard_timeout = 0
  760.  
  761. msg.match.dl_type = 0x0800
  762.  
  763. msg.match.nw_dst = "10.0.0.5"
  764.  
  765. msg.actions.append(of.ofp_action_output(port = 5))
  766.  
  767. event.connection.send(msg)
  768.  
  769. msg = of.ofp_flow_mod()
  770.  
  771. msg.priority =100
  772.  
  773. msg.idle_timeout = 0
  774.  
  775. msg.hard_timeout = 0
  776.  
  777. msg.match.dl_type = 0x0800
  778.  
  779. msg.match.nw_dst = "10.0.0.6"
  780.  
  781. msg.actions.append(of.ofp_action_output(port = 6))
  782.  
  783. event.connection.send(msg)
  784.  
  785. def launch ():
  786.  
  787. global start_time
  788.  
  789. core.openflow.addListenerByName("PortStatsReceived",_handle_portstats_received)
  790.  
  791. core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)
  792.  
  793. 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. # 创建一个基于拓扑类的MyTopo类使得多交换机连接多个主机和交换机的复杂拓扑
  28.  
  29. class MyTopo(Topo):
  30.  
  31. "Single switch connected to n hosts."
  32. # 构造函数创建5个交换机和6个主机
  33.  
  34. def __init__(self):
  35.  
  36. Topo.__init__(self)
  37.  
  38. s1=self.addSwitch('s1')
  39.  
  40. s2=self.addSwitch('s2')
  41.  
  42. s3=self.addSwitch('s3')
  43.  
  44. s4=self.addSwitch('s4')
  45.  
  46. s5=self.addSwitch('s5')
  47.  
  48. h1=self.addHost('h1')
  49.  
  50. h2=self.addHost('h2')
  51.  
  52. h3=self.addHost('h3')
  53.  
  54. h4=self.addHost('h4')
  55.  
  56. h5=self.addHost('h5')
  57.  
  58. h6=self.addHost('h6')
  59.  
  60. # 主机1,2,3连接s1
  61.  
  62. self.addLink(h1, s1, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  63.  
  64. self.addLink(h2, s1, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  65.  
  66. self.addLink(h3, s1, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  67.  
  68. # 交换机2,3,4连接s1,所以说s1有6个接口
  69.  
  70. self.addLink(s1, s2, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  71.  
  72. self.addLink(s1, s3, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  73.  
  74. self.addLink(s1, s4, bw=1, delay='10ms', loss=0, max_queue_size=1000use_htb=True)
  75.  
  76. # 交换机2,3,4连接s5,所以说s5也有6个接口,2,3,4各两个
  77.  
  78. self.addLink(s2, s5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  79.  
  80. self.addLink(s3, s5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  81.  
  82. self.addLink(s4, s5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  83.  
  84. # 主机4,5,6连接s5
  85.  
  86. self.addLink(s5, h4, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  87.  
  88. self.addLink(s5, h5, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  89.  
  90. self.addLink(s5, h6, bw=1, delay='10ms', loss=0, max_queue_size=1000, use_htb=True)
  91.  
  92. def perfTest():
  93.  
  94. # 创建网络并运行简单性能测试
  95.  
  96. topo = MyTopo()
  97.  
  98. net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink, controller=partial(RemoteController, ip='10.0.0.13', port=6633))
  99.  
  100. net.start()
  101.  
  102. print "Dumping host connections"
  103.  
  104. dumpNodeConnections(net.hosts)
  105.  
  106. h1,h2,h3=net.get('h1','h2','h3')
  107.  
  108. h4,h5,h6=net.get('h4','h5','h6')
  109.  
  110. h1.setMAC("0:0:0:0:0:1")
  111.  
  112. h2.setMAC("0:0:0:0:0:2")
  113.  
  114. h3.setMAC("0:0:0:0:0:3")
  115.  
  116. h4.setMAC("0:0:0:0:0:4")
  117.  
  118. h5.setMAC("0:0:0:0:0:5")
  119.  
  120. h6.setMAC("0:0:0:0:0:6")
  121.  
  122. CLI(net)
  123.  
  124. net.stop()
  125.  
  126. if __name__ == '__main__':
  127.  
  128. setLogLevel('info')
  129.  
  130. perfTest()

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

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

rulemininet.py脚本的显示情况:

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

而我的是。。

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

总结:

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

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

Mininet系列实验(六):Mininet动态改变转发规则实验的更多相关文章

  1. mininet实验 动态改变转发规则实验

    写在前面 本实验参考 POX脚本设置好控制器的转发策略,所以只要理解脚本. mininet脚本设置好拓扑和相关信息,所以也只要理解脚本. POX脚本目前基本看不懂. 本实验我学会了:POX控制器Web ...

  2. Mininet实验 动态改变转发规则

    介绍 拓扑如下: 在该环境下,假设H1 ping H4,初始的路由规则是S1-S2-S5,一秒后,路由转发规则变为S1-S3-S5,再过一秒,规则变为S1-S4-S5,然后再回到最初的转发规则S1-S ...

  3. Mininet 系列实验(六)

    写在前面 这次实验遇到了非常多问题,非常非常多,花了很多时间去解决,还是有一些小问题没有解决,但是基本上能完成实验.建议先看完全文再开始做实验. 实验内容 先看一下本次实验的拓扑图: 在该环境下,假设 ...

  4. SSH的本地、远程、动态端口转发实验笔记

    SSH端口转发 SSH 会自动加密和解密所有 SSH 客户端与服务端之间的网络数据.但是,SSH 还能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的加密及解密服务.这一 ...

  5. Mininet 系列实验(四)

    实验内容 本次实验拓扑图: 在该环境下,h0 向 h1 发送数据包,由于在 mininet 脚本中设置了连接损耗率,在传输过程中会丢失一些包,本次实验的目的是展示如何通过控制器计算路径损耗速率(h0- ...

  6. Mininet 系列实验(三)

    实验内容 基础 Mininet 可视化界面进行自定义拓扑及拓扑设备自定义设置,实现自定义脚本应用. 参考 Mininet可视化应用 实验环境 虚拟机: Oracle VM VirtualBox Ubu ...

  7. Mininet 系列实验(一)

    关于SDN的第一个实验,似乎实验室里的前辈们也都是从这里开始的. 实验内容 使用源码安装Mininet 参考 Mininet使用源码安装 实验环境 虚拟机:Oracle VM VirtualBox U ...

  8. Mininet系列实验(五):Mininet设置带宽之简单性能测试

    1.实验目的 该实验通过Mininet学习python自定义拓扑实现,可在python脚本文件中设计任意想要的拓扑,简单方便,并通过设置交换机和主机之间链路的带宽.延迟及丢包率,测试主机之间的性能.在 ...

  9. Mininet系列实验(二):Mininet可视化应用

    1 实验目的 该实验通过Mininet学习miniedit可视化操作,可直接在界面上编辑任意想要的拓扑,生成python自定义拓扑脚本,简单方便.在实验过程中,可以了解以下方面的知识: Miniedi ...

随机推荐

  1. python识别文字tesseract

    Ubuntu版本: .tesseract-ocr安装 sudo apt-get install tesseract-ocr .pytesseract安装 sudo pip install pytess ...

  2. 一语道破Java 11的ZGC为何如此高效

    GC是大部分现代语言内置的特性,Java 11 新加入的ZGC号称可以达到10ms 以下的 GC 停顿,本文作者对这一新功能进行了深入解析.同时还对还对这一新功能带来的其他可能性做了展望.ZGC是否可 ...

  3. Spring Boot 2发送邮件手把手图文教程

    原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...

  4. 基于STM32L476开发板的USB音频设备

    现代音频设备中有很多知识产权. 我想研究创建一个与手机交互的算法设备(运行non-trivial算法的嵌入式设备). 我发现创建一个Lightning设备比创建一个连接到Android手机的的USB设 ...

  5. python函数式编程-偏向函数

    Python的functools模块提供了很多有用的功能,其中一个就是偏函数(Partial function).要注意,这里的偏函数和数学意义上的偏函数不一样. 在介绍函数参数的时候,我们讲到,通过 ...

  6. [课本10.1.4]JDBC数据库连接池- C3P0数据源--通过构造方法创建数据源对象--通过配置文件创建数据源对象[推荐]

    JDBC- C3P0数据源 /*重点提醒*/ 连接数据库的较低的jar包版本会与较高版本的mysql版本有冲突; 通过把mysql 8.0的版本降到5.5, jar包仍使用较高的 mysql-conn ...

  7. Vue -- filters 过滤器、倒计时效果

    局部过滤器 时间.***号 <div id="wrapper" class="wrapper" style="display: none;&qu ...

  8. stm32 HardFault_Handler调试及问题查找方法

    STM32出现HardFault_Handler故障的原因主要有两个方面: 1.内存溢出或者访问越界.这个需要自己写程序的时候规范代码,遇到了需要慢慢排查. 2.堆栈溢出.增加堆栈的大小. 出现问题时 ...

  9. Codeforces Global Round 2 E. Pavel and Triangles(思维+DP)

    题目链接:https://codeforces.com/contest/1119/problem/E 题意:有n种长度的棍子,有a_i根2^i长度的棍子,问最多可以组成多少个三角形 题解:dp[i]表 ...

  10. mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )

    刚刚向数据库插入数据的时候出现了这么一段错误 Deadlock found when trying to get lock; try restarting transaction 主要原因(由于无法使 ...