Mininet实验 动态改变转发规则

介绍

拓扑如下:

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


pox脚本

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  66 def getTheTime(): # fuction to create a timestamp 67  68 flock = time.localtime() 69  70 then = "[%s-%s-%s" % (str(flock.tm_year), 71  str(flock.tm_mon), str(flock.tm_mday)) 72  73 if int(flock.tm_hour) < 10: 74  75 hrs = "0%s" % (str(flock.tm_hour)) 76  77 else: 78  79 hrs = str(flock.tm_hour) 80  81 if int(flock.tm_min) < 10: 82  83 mins = str(flock.tm_min) 84  85 secs = "0%s" % (str(flock.tm_sec)) 86  87 else: 88  89 secs = str(flock.tm_sec) 90  91 then += "]%s.%s.%s" % (hrs, mins, secs) 92  93 return then 94  95  96 def _timer_func(): 97 global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid, turn 98  99 # print getTheTime(), "sent the port stats request to s1_dpid"100 101 if turn == 0:102 msg = of.ofp_flow_mod()103 104 msg.command = of.OFPFC_MODIFY_STRICT105 106 msg.priority = 100107 108 msg.idle_timeout = 0109 110 msg.hard_timeout = 0111 112 msg.match.dl_type = 0x0800113 114 msg.match.nw_dst = "10.0.0.4"115 116 msg.actions.append(of.ofp_action_output(port=5))117 118  core.openflow.getConnection(s1_dpid).send(msg)119 120 turn = 1121 122 return123 124 if turn == 1:125 msg = of.ofp_flow_mod()126 127 msg.command = of.OFPFC_MODIFY_STRICT128 129 msg.priority = 100130 131 msg.idle_timeout = 0132 133 msg.hard_timeout = 0134 135 msg.match.dl_type = 0x0800136 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 = 2144 return145 146 if turn == 2:147 msg = of.ofp_flow_mod()148 149 msg.command = of.OFPFC_MODIFY_STRICT150 151 msg.priority = 100152 153 msg.idle_timeout = 0154 155 msg.hard_timeout = 0156 157 msg.match.dl_type = 0x0800158 159 msg.match.nw_dst = "10.0.0.4"160 161 msg.actions.append(of.ofp_action_output(port=4))162 163 turn = 0164 165 return166 167 168 def _handle_portstats_received(event):169 global s1_p1, s1_p4, s1_p5, s1_p6, s2_p1, s3_p1, s4_p1170 171 global pre_s1_p1, pre_s1_p4, pre_s1_p5, pre_s1_p6, pre_s2_p1, pre_s3_p1, pre_s4_p1172 173 if event.connection.dpid == s1_dpid:174 175 for f in event.stats:176 177 if int(f.port_no) < 65534:178 179 if f.port_no == 1:180 pre_s1_p1 = s1_p1181 182 s1_p1 = f.rx_packets183 184 if f.port_no == 4:185 pre_s1_p4 = s1_p4186 187 s1_p4 = f.tx_packets188 189 # s1_p4=f.tx_bytes190 191 if f.port_no == 5:192 pre_s1_p5 = s1_p5193 194 s1_p5 = f.tx_packets195 196 if f.port_no == 6:197 pre_s1_p6 = s1_p6198 199 s1_p6 = f.tx_packets200 201 for f in event.stats:202 203 if int(f.port_no) < 65534:204 205 if f.port_no == 1:206 pre_s2_p1 = s2_p1207 208 s2_p1 = f.rx_packets209 210 # s2_p1=f.rx_bytes211 212 if event.connection.dpid == s3_dpid:213 214 for f in event.stats:215 216 if int(f.port_no) < 65534:217 218 if f.port_no == 1:219 pre_s3_p1 = s3_p1220 221 s3_p1 = f.rx_packets222 223 if event.connection.dpid == s4_dpid:224 225 for f in event.stats:226 227 if int(f.port_no) < 65534:228 229 if f.port_no == 1:230 pre_s4_p1 = s4_p1231 232 s4_p1 = f.rx_packets233 234 235 def _handle_ConnectionUp(event):236 global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid237 238 print "ConnectionUp: ", dpidToStr(event.connection.dpid)239 240 # remember the connection dpid for switch241 242 for m in event.connection.features.ports:243 244 if m.name == "s1-eth1":245 246 s1_dpid = event.connection.dpid247 248 print "s1_dpid=", s1_dpid249 250 elif m.name == "s2-eth1":251 252 s2_dpid = event.connection.dpid253 254 print "s2_dpid=", s2_dpid255 256 elif m.name == "s3-eth1":257 258 s3_dpid = event.connection.dpid259 260 print "s3_dpid=", s3_dpid261 262 elif m.name == "s4-eth1":263 264 s4_dpid = event.connection.dpid265 266 print "s4_dpid=", s4_dpid267 268 elif m.name == "s5-eth1":269 270 s5_dpid = event.connection.dpid271 272 print "s5_dpid=", s5_dpid273 274 if s1_dpid <> 0 and s2_dpid <> 0 and s3_dpid <> 0 and s4_dpid <> 0:275 Timer(1, _timer_func, recurring=True)276 277 278 def _handle_PacketIn(event):279 global s1_dpid, s2_dpid, s3_dpid, s4_dpid, s5_dpid280 281 packet = event.parsed282 283 if event.connection.dpid == s1_dpid:284 285 a = packet.find(arp)286 287 if a and a.protodst == "10.0.0.4":288 msg = of.ofp_packet_out(data=event.ofp)289 290 msg.actions.append(of.ofp_action_output(port=4))291 292  event.connection.send(msg)293 294 if a and a.protodst == "10.0.0.5":295 msg = of.ofp_packet_out(data=event.ofp)296 297 msg.actions.append(of.ofp_action_output(port=5))298 299  event.connection.send(msg)300 301 if a and a.protodst == "10.0.0.6":302 msg = of.ofp_packet_out(data=event.ofp)303 304 msg.actions.append(of.ofp_action_output(port=6))305 306  event.connection.send(msg)307 308 if a and a.protodst == "10.0.0.1":309 msg = of.ofp_packet_out(data=event.ofp)310 311 msg.actions.append(of.ofp_action_output(port=1))312 313  event.connection.send(msg)314 315 if a and a.protodst == "10.0.0.2":316 msg = of.ofp_packet_out(data=event.ofp)317 318 msg.actions.append(of.ofp_action_output(port=2))319 320  event.connection.send(msg)321 322 if a and a.protodst == "10.0.0.3":323 msg = of.ofp_packet_out(data=event.ofp)324 325 msg.actions.append(of.ofp_action_output(port=3))326 327  event.connection.send(msg)328 329 msg = of.ofp_flow_mod()330 331 msg.priority = 100332 333 msg.idle_timeout = 0334 335 msg.hard_timeout = 0336 337 msg.match.dl_type = 0x0800338 339 msg.match.nw_dst = "10.0.0.1"340 341 msg.actions.append(of.ofp_action_output(port=1))342 343  event.connection.send(msg)344 345 msg = of.ofp_flow_mod()346 347 msg.priority = 100348 349 msg.idle_timeout = 0350 351 msg.hard_timeout = 0352 353 msg.match.dl_type = 0x0800354 355 msg.match.nw_dst = "10.0.0.2"356 357 msg.actions.append(of.ofp_action_output(port=2))358 359  event.connection.send(msg)360 361 msg = of.ofp_flow_mod()362 363 msg.priority = 100364 365 msg.idle_timeout = 0366 367 msg.hard_timeout = 0368 369 msg.match.dl_type = 0x0800370 371 msg.match.nw_dst = "10.0.0.3"372 373 msg.actions.append(of.ofp_action_output(port=3))374 375  event.connection.send(msg)376 377 msg = of.ofp_flow_mod()378 379 msg.priority = 100380 381 msg.idle_timeout = 0382 383 msg.hard_timeout = 1384 385 msg.match.dl_type = 0x0800386 387 msg.match.nw_dst = "10.0.0.4"388 389 msg.actions.append(of.ofp_action_output(port=4))390 391  event.connection.send(msg)392 393 msg = of.ofp_flow_mod()394 395 msg.priority = 100396 397 msg.idle_timeout = 0398 399 msg.hard_timeout = 0400 401 msg.match.dl_type = 0x0800402 403 msg.match.nw_dst = "10.0.0.5"404 405 msg.actions.append(of.ofp_action_output(port=5))406 407  event.connection.send(msg)408 409 msg = of.ofp_flow_mod()410 411 msg.priority = 100412 413 msg.idle_timeout = 0414 415 msg.hard_timeout = 0416 417 msg.match.dl_type = 0x0800418 419 msg.match.nw_dst = "10.0.0.6"420 421 msg.actions.append(of.ofp_action_output(port=6))422 423  event.connection.send(msg)424 425 426 427 elif event.connection.dpid == s2_dpid:428 429 msg = of.ofp_flow_mod()430 431 msg.priority = 10432 433 msg.idle_timeout = 0434 435 msg.hard_timeout = 0436 437 msg.match.in_port = 1438 439 msg.match.dl_type = 0x0806440 441 msg.actions.append(of.ofp_action_output(port=2))442 443  event.connection.send(msg)444 445 msg = of.ofp_flow_mod()446 447 msg.priority = 10448 449 msg.idle_timeout = 0450 451 msg.hard_timeout = 0452 453 msg.match.in_port = 1454 455 msg.match.dl_type = 0x0800456 457 msg.actions.append(of.ofp_action_output(port=2))458 459  event.connection.send(msg)460 461 msg = of.ofp_flow_mod()462 463 msg.priority = 10464 465 msg.idle_timeout = 0466 467 msg.hard_timeout = 0468 469 msg.match.in_port = 2470 471 msg.match.dl_type = 0x0806472 473 msg.actions.append(of.ofp_action_output(port=1))474 475  event.connection.send(msg)476 477 msg = of.ofp_flow_mod()478 479 msg.priority = 10480 481 msg.idle_timeout = 0482 483 msg.hard_timeout = 0484 485 msg.match.in_port = 2486 487 msg.match.dl_type = 0x0800488 489 msg.actions.append(of.ofp_action_output(port=1))490 491  event.connection.send(msg)492 493 494 495 elif event.connection.dpid == s3_dpid:496 497 msg = of.ofp_flow_mod()498 499 msg.priority = 10500 501 msg.idle_timeout = 0502 503 msg.hard_timeout = 0504 505 msg.match.in_port = 1506 507 msg.match.dl_type = 0x0806508 509 msg.actions.append(of.ofp_action_output(port=2))510 511  event.connection.send(msg)512 513 msg = of.ofp_flow_mod()514 515 msg.priority = 10516 517 msg.idle_timeout = 0518 519 msg.hard_timeout = 0520 521 msg.match.in_port = 1522 523 msg.match.dl_type = 0x0800524 525 msg.actions.append(of.ofp_action_output(port=2))526 527  event.connection.send(msg)528 529 msg = of.ofp_flow_mod()530 531 msg.priority = 10532 533 msg.idle_timeout = 0534 535 msg.hard_timeout = 0536 537 msg.match.in_port = 2538 539 msg.match.dl_type = 0x0806540 541 msg.actions.append(of.ofp_action_output(port=1))542 543  event.connection.send(msg)544 545 msg = of.ofp_flow_mod()546 547 msg.priority = 10548 549 msg.idle_timeout = 0550 551 msg.hard_timeout = 0552 553 msg.match.in_port = 2554 555 msg.match.dl_type = 0x0800556 557 msg.actions.append(of.ofp_action_output(port=1))558 559  event.connection.send(msg)560 561 562 563 elif event.connection.dpid == s4_dpid:564 565 msg = of.ofp_flow_mod()566 567 msg.priority = 10568 569 msg.idle_timeout = 0570 571 msg.hard_timeout = 0572 573 msg.match.in_port = 1574 575 msg.match.dl_type = 0x0806576 577 msg.actions.append(of.ofp_action_output(port=2))578 579  event.connection.send(msg)580 581 msg = of.ofp_flow_mod()582 583 msg.priority = 10584 585 msg.idle_timeout = 0586 587 msg.hard_timeout = 0588 589 msg.match.in_port = 1590 591 msg.match.dl_type = 0x0800592 593 msg.actions.append(of.ofp_action_output(port=2))594 595  event.connection.send(msg)596 597 msg = of.ofp_flow_mod()598 599 msg.priority = 10600 601 msg.idle_timeout = 0602 603 msg.hard_timeout = 0604 605 msg.match.in_port = 2606 607 msg.match.dl_type = 0x0806608 609 msg.actions.append(of.ofp_action_output(port=1))610 611  event.connection.send(msg)612 613 msg = of.ofp_flow_mod()614 615 msg.priority = 10616 617 msg.idle_timeout = 0618 619 msg.hard_timeout = 0620 621 msg.match.in_port = 2622 623 msg.match.dl_type = 0x0800624 625 msg.actions.append(of.ofp_action_output(port=1))626 627  event.connection.send(msg)628 629 630 631 elif event.connection.dpid == s5_dpid:632 633 a = packet.find(arp)634 635 if a and a.protodst == "10.0.0.4":636 msg = of.ofp_packet_out(data=event.ofp)637 638 msg.actions.append(of.ofp_action_output(port=4))639 640  event.connection.send(msg)641 642 if a and a.protodst == "10.0.0.5":643 msg = of.ofp_packet_out(data=event.ofp)644 645 msg.actions.append(of.ofp_action_output(port=5))646 647  event.connection.send(msg)648 649 if a and a.protodst == "10.0.0.6":650 msg = of.ofp_packet_out(data=event.ofp)651 652 msg.actions.append(of.ofp_action_output(port=6))653 654  event.connection.send(msg)655 656 if a and a.protodst == "10.0.0.1":657 msg = of.ofp_packet_out(data=event.ofp)658 659 msg.actions.append(of.ofp_action_output(port=1))660 661  event.connection.send(msg)662 663 if a and a.protodst == "10.0.0.2":664 msg = of.ofp_packet_out(data=event.ofp)665 666 msg.actions.append(of.ofp_action_output(port=2))667 668  event.connection.send(msg)669 670 if a and a.protodst == "10.0.0.3":671 msg = of.ofp_packet_out(data=event.ofp)672 673 msg.actions.append(of.ofp_action_output(port=3))674 675  event.connection.send(msg)676 677 msg = of.ofp_flow_mod()678 679 msg.priority = 100680 681 msg.idle_timeout = 0682 683 msg.hard_timeout = 0684 685 msg.match.dl_type = 0x0800686 687 msg.match.nw_dst = "10.0.0.1"688 689 msg.actions.append(of.ofp_action_output(port=1))690 691  event.connection.send(msg)692 693 msg = of.ofp_flow_mod()694 695 msg.priority = 100696 697 msg.idle_timeout = 0698 699 msg.hard_timeout = 0700 701 msg.match.dl_type = 0x0800702 703 msg.match.nw_dst = "10.0.0.2"704 705 msg.actions.append(of.ofp_action_output(port=2))706 707  event.connection.send(msg)708 709 msg = of.ofp_flow_mod()710 711 msg.priority = 100712 713 msg.idle_timeout = 0714 715 msg.hard_timeout = 0716 717 msg.match.dl_type = 0x0800718 719 msg.match.nw_dst = "10.0.0.3"720 721 msg.actions.append(of.ofp_action_output(port=3))722 723  event.connection.send(msg)724 725 msg = of.ofp_flow_mod()726 727 msg.priority = 100728 729 msg.idle_timeout = 0730 731 msg.hard_timeout = 0732 733 msg.match.dl_type = 0x0800734 735 msg.match.nw_dst = "10.0.0.4"736 737 msg.actions.append(of.ofp_action_output(port=4))738 739  event.connection.send(msg)740 741 msg = of.ofp_flow_mod()742 743 msg.priority = 100744 745 msg.idle_timeout = 0746 747 msg.hard_timeout = 0748 749 msg.match.dl_type = 0x0800750 751 msg.match.nw_dst = "10.0.0.5"752 753 msg.actions.append(of.ofp_action_output(port=5))754 755  event.connection.send(msg)756 757 msg = of.ofp_flow_mod()758 759 msg.priority = 100760 761 msg.idle_timeout = 0762 763 msg.hard_timeout = 0764 765 msg.match.dl_type = 0x0800766 767 msg.match.nw_dst = "10.0.0.6"768 769 msg.actions.append(of.ofp_action_output(port=6))770 771  event.connection.send(msg)772 773 774 def launch():775 global start_time776 777 core.openflow.addListenerByName("PortStatsReceived", _handle_portstats_received)778 779 core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)780 781 core.openflow.addListenerByName("PacketIn", _handle_PacketIn)

 


mininet脚本

 1 #!/usr/bin/python 2  3  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  29  30  31 class MyTopo(Topo): 32  33 "Single switch connected to n hosts." 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  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 self.addLink(s1, s2, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  70  71 self.addLink(s1, s3, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  72  73 self.addLink(s1, s4, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  74  75 self.addLink(s2, s5, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  76  77 self.addLink(s3, s5, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  78  79 self.addLink(s4, s5, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  80  81 self.addLink(s5, h4, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  82  83 self.addLink(s5, h5, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  84  85 self.addLink(s5, h6, bw=1, delay=10ms, loss=0, max_queue_size=1000, use_htb=True)  86  87  88  89 def perfTest(): 90  91 "Create network and run simple performance test" 92  93 topo = MyTopo() 94  95  96 net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink, controller=partial(RemoteController, ip=127.0.0.1, port=6633)) 97  98  net.start() 99 100 print "Dumping host connections"101 102  dumpNodeConnections(net.hosts)103 104 h1,h2,h3=net.get(h1,h2,h3)105 106 h4,h5,h6=net.get(h4,h5,h6)107 108 h1.setMAC("0:0:0:0:0:1")109 110 h2.setMAC("0:0:0:0:0:2")111 112 h3.setMAC("0:0:0:0:0:3")113 114 h4.setMAC("0:0:0:0:0:4")115 116 h5.setMAC("0:0:0:0:0:5")117 118 h6.setMAC("0:0:0:0:0:6")119 120  CLI(net)121 122  net.stop()123 124 125 126 if __name__ == __main__:127 128 setLogLevel(info)129 130 perfTest()

 

 

如果是在不同的机器使用的pox将控制器ip修改到对应pox所在的ip,我使用的是同一台计算机上的就直接修改成了127.0.0.1。


然后运行pox脚本再运行mininet脚本,得到下图:

mininet运行没啥问题,不过提示0丢包率是非法的emmmmm。 然后pox的内容有些奇怪,咋感觉少了一句的感觉。强迫症,到脚本里填上。在第260行填上一行输出。得到下图:

鉴于有人说,h1 ping不到 h4,就先pingall一下,得到下图:

一切正常。 然后继续实验,在mininet输入h1 ping -i 0.1 h4 每秒从h1传送10个包到h4。

mininet界面到时挺正常的,但是pox界面emmm一片死寂,所以我反复了几次,结果没有变化。估计不是pox出问题了,就是pox脚本写的有问题吧,难不成是我实验环境有问题?

由于对脚本内容了解的不够,所以我先尝试了一下使用poxdesk图形界面来显示结果。先安装一下。

1 cd ./pox/ext2 git clone https://github.com/MurphyMc/poxdesk3 cd poxdesk4 wget http://downloads.sourceforge.net/qooxdoo/qooxdoo-2.0.2-sdk.zip5 unzip qooxdoo-2.0.2-sdk.zip6 mv qooxdoo-2.0.2-sdk qx7 cd poxdesk8 ./generate.py

我的小水管算是把我卡死了

然后通过命令运行:

1 ./pox.py 脚本名 web messenger messenger.log_service messenger.ajax_transport openflow.of_service poxdesk

 

然后进入浏览器查看:http://pox-ip:8000/poxdesk。这里pox-ip是pox所在的ip,如果本地就直接使用127.0.0.1就行。

观察s1目标为h4的,发现output一直在5和6之间变化,但是理论来说应该有三种不同的路由规则。结果很尴尬。。。

相关文章