{"id":17205152,"url":"https://github.com/drnpkr/sdn-lab-assignment","last_synced_at":"2025-09-07T14:05:26.707Z","repository":{"id":176815085,"uuid":"160010435","full_name":"drnpkr/sdn-lab-assignment","owner":"drnpkr","description":"Assignment aimed at setting up and configuring a network of SDN-enabled devices","archived":false,"fork":false,"pushed_at":"2018-12-02T04:36:07.000Z","size":1302,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T09:26:27.714Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drnpkr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-02T03:37:25.000Z","updated_at":"2024-01-30T22:02:49.000Z","dependencies_parsed_at":"2023-06-28T11:15:44.188Z","dependency_job_id":null,"html_url":"https://github.com/drnpkr/sdn-lab-assignment","commit_stats":null,"previous_names":["drnpkr/sdn-lab-assignment"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drnpkr%2Fsdn-lab-assignment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drnpkr%2Fsdn-lab-assignment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drnpkr%2Fsdn-lab-assignment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drnpkr%2Fsdn-lab-assignment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drnpkr","download_url":"https://codeload.github.com/drnpkr/sdn-lab-assignment/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245440577,"owners_count":20615633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T02:24:27.409Z","updated_at":"2025-03-25T10:16:41.336Z","avatar_url":"https://github.com/drnpkr.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SDN Lab Assignment\n\nThis Lab Assignment was part of the NWEN302 Course (Computer Network Design) at the [School of Engineering and Computer Science](https://www.victoria.ac.nz/ecs), Victoria University of Wellington, New Zealand.\n\n**Assignment Objectives:** Setup and configure a network of SDN-enabled devices; operate the network and compare with traditional IP network.\n\n**Last updated: December 2018**\n\n## Introduction\n---\n\nSDN is a new approach to the current world of networking, in this lab you will learn basic concepts of SDN through OpenFlow. OpenFlow started with several engineers from Stanford University creating a protocol that would have a logically centralised control plane separated from the underlying switching details. OpenFlow was architected for a number of devices containing only data planes to respond to commands sent to them from a logically centralised controller that housed the single control plane for that network. The controller is responsible for maintaining all of the network paths, as well as programming each of the network devices it controlled. The commands and responses to those commands are described in the OpenFlow protocol.\n\n## Background reading\n---\n\nBefore starting this lab, read up on the technologies you will be using:\n\n1. Get familiar with the SDN emulation environment, [Mininet](http://mininet.org/walkthrough/). (30 minutes – 1 hour)\n2. Refresh your Python programming skills using the [Python Tutorial](http://docs.python.org/tutorial/). (1+ hour )\n3. Study the [RYU Controller Tutorial](attachments/RYU-Controller-Tutorial.pdf). (~ 2 hours)\n      *   [Pseudo-code.pdf](attachments/Pseudo-code.pdf)\n      *   [Useful-mininet-setups.pdf](attachments/Useful-mininet-setups.pdf)\n      *    **Note that sdnhub.org is not operable. All these documents have been fetched from web archive.**\n\n## Laboratory environment\n---\n\nIn this lab, you will start by learning the basics of running Mininet in a VirtualBox virtual machine. Mininet facilitates creating and manipulating Software Defined Networking components.\n\nThrough Mininet you will explore OpenFlow, which is an open interface for controlling the network elements through their forwarding tables. A network element may be converted into a switch, router or even an access points via low-level primitives defined in OpenFlow. This lab is your opportunity to gain hands-on experience with the platforms and debugging tools most useful for developing network control applications on OpenFlow.\n\nTo configure and install your own copy of the VirtualBox appliance (virtual machine) follow [these instructions](attachments/import-ova.pdf).\n\n\u003c!--\n\nIf you have troubles with ssh to guest VM from host machine, the following can help:\n\n   1 Add host-only network. In `File Menu-\u003ePreferences-\u003eNetwork` click on the \"Host-only Networks\" tab and than add a new host-only network (with default settings) by clicking on the `+` icon (you may already have a network called `vboxnet0`, in such a case you can skip this step).\n   1 Select your VM and go to the Settings tab. Go to Network-\u003eAdapter 1. Select the \"Enable Adapter\" box and attach it to \"Host-only network\" created at the step 1.\n\n--\u003e\n\nOnce imported, the user name is `nwen302` with a password of `nwen302`.\n\nImportant directories include:\n\n```bash\n/usr/local/lib/python2.7/dist-packages/ryu                                                 \n                                          /app     \n                                          /base\n                                          /....\n                                          /controller\n                                          /lib\n                                          /ofproto\n                                          /....\n                                          /topology\n```\n\n`app` : A set of applications that run on-top of the controller.\n\n`base` : Contains the base class for RYU applications. The RyuApp class in the app_manager.py file is inherited when creating a new application.\n\n`controller` : The required set of files to handle OpenFlow functions (e.g., packets from switches, generating flows, handling network events, gathering statistics etc).\n\n`lib` : Packet libraries to parse different protocol headers and a library for OFConfig. In addition, it includes parsers for Netflow and sFlow too.\n\n`ofproto` : OpenFlow protocol specific information and related parsers to support different versions of OF protocol (1.0, 1.2, 1.3, 1.4)\n\n`topology` : Code that performs topology discovery related to OpenFlow switches and handles associated information (e.g., ports, links etc). Internally uses LLDP protocol.\n\n### Network topology\n---\n\nThe topology has three hosts named h1, h2 and h3 respectively. Each host has an Ethernet interface called h1-eth0, h2-eth0 and h3-eth0 respectively. The three hosts are connected through a switch named s1.\n\nThe switch s1 has three ports named s1-eth1, s1-eth2 and s1-eth3. The controller is connected on the loopback interface (in real life this may or may not be the case, it means the switch and controller are built in a single box).\n\nThe controller is identified as c0 and connected through port 6653.\n\n\u003cimg alt='Lab4.Topology.png' src='attachments/Lab4.Topology.png' align='center' title='Lab4.Topology.png' /\u003e\n\n### Creating a test emulated network\n---\n\n**Before we start, please note the following:**\n   * `$` preceeds Linux commands that should be typed at the shell prompt (as user nwen302),\n   * `mininet\u003e` preceeds Mininet commands that should be typed at Mininet’s CLI,\n   * `#` preceeds Linux commands that are typed at a root shell prompt.\n\n1. In the Virtual Machine, create the network with the following command:\n\n```bash\n$ sudo mn --topo=single,3 --mac\n```\n\nNote that since we are not providing here an external controller, mininet will create its own.\n\n2. To verify that the network was successfully created, within the mininet prompt:\n\n```bash\nmininet\u003e net\nmininet\u003e dump\n```\n\nYou should be able to determine from the output if the network is indeed as in the topology diagram as shown above.\n\n#### Accessing each host\n---\n\n1. To check the network interfaces on h1\n\n```bash\nmininet\u003e h1 ifconfig\n```\n\n2. To ping another host from h1\n\n```bash\nmininet\u003e h1 ping h3\nmininet\u003e h2 ping h1\n```\n\n3. To ping all hosts from all hosts\n\n```bash\nmininet\u003e pingall\n```\n\n#### Housekeeping in mininet\n---\n\nTo exit and clear devices\n\n```bash\nmininet\u003e exit\n$ sudo mn -c\n```\n\n### Creating the emulated network for the Lab - Connecting to Ryu Controller\n---\n\n\u003c!--\n\nTo ensure that no other controller is present:\n\n\u003cverbatim\u003e\n$ sudo killall controller\n\u003c/verbatim\u003e\n\nNote that this controller is a simple OpenFlow reference controller implementation in linux.\n--\u003e\n\nIn a new terminal session, start the Ryu controller:\n\n```bash\n$ sudo ryu run /usr/local/lib/python2.7/dist-packages/ryu/app/simple_switch_13.py\n```\n\nAlternatively, you can also start the controller using ryu-manager:\n\n```bash\n$ sudo ryu-manager /usr/local/lib/python2.7/dist-packages/ryu/app/simple_switch_13.py\n```\n\nTo get more detailed notifications about the controller stat you can add the `--verbose` option:\n\n```bash\n$ sudo ryu run /usr/local/lib/python2.7/dist-packages/ryu/app/simple_switch_13.py --verbose\n```\n\nClear all mininet components\n\n```bash\n$ sudo mn -c\n```\n\nStart mininet with remote controller:\n\n```bash\n$ sudo mn --controller=remote --topo=single,3 --switch=ovsk,protocols=OpenFlow13 --mac\n```\n\nWith the optional argument `--controller=remote`, it defaults to `localhost:6653`. By the option `--switch=ovsk,protocols=OpenFlow13` we tell Mininet to use OpenFlow version 1.3. `--mac` will tell Mininet to create hosts with easily readable MAC addresses.\n\n### All about flows\n---\n\nA flow is the finest grained work unit of a switch.\n\nIn your VM emulated network environment there is a number of utilities enabling visibility into the network, its hosts, network devices and their flow tables. They can be especially useful for debugging by viewing flow state and flow counters. \n\nAssuming you have already created an emulated network and connected it to a Ryu controller (as showed in previous section), in a separate terminal window (e.g. using `screen` or another `ssh` session) you can run the following commands.\n\nTo print a brief overview of the database contents (information about all the virtual switches):\n\n```bash\n$ sudo ovs-vsctl show\n```\n\nTo see more options for command `ovs-vsctl`:\n\n```bash\n$ man ovs-vsctl\n```\n\nTo print to the console information on `switch number 1`, including information on its flow tables and ports:\n\n```bash\n$ sudo ovs-ofctl -O OpenFlow13 show s1\n```\n\nTo prints to the console statistics for network devices associated with `switch number 1`:\n\n```bash\n$ sudo ovs-ofctl -O OpenFlow13 dump-ports s1\n```\n\nTo print to the console all flow entries in tables of `switch number 1`:\n\n```bash\n$ sudo ovs-ofctl -O OpenFlow13 dump-flows s1\n```\n\nTo see more options for command `ovs-ofctl`:\n\n```bash\n$ man ovs-ofctl\n```\n\n### Developing RYU applications\n---\n\nRyu is written fully in python script. To develop your own aplication you can start with opening related files in the `.../ryu/app` folder with your favorite editor (which is vi) and edit them. Do not forget to restart ryu after making any changes.\n\nTo get some ideas about developing applications you can start with [this tutorial](http://ryu.readthedocs.io/en/latest/writing_ryu_app.html).\n\n#### Packets with the RYU packet library\n---\n\nRyu also includes [packet parser library](http://ryu.readthedocs.io/en/latest/library_packet.html) **which may come in handy when working on the TASKS**.\n\n### The `simple_switch_13.py` file\n---\n\nThe `simple_switch_13.py` file is located in your virtual machine in the following folder:\n\n```bash\n/usr/local/lib/python2.7/dist-packages/ryu/app\n```\n\nThis file can be also located in the [attachments directory](attachments/simple_switch_13.py). For the latest version of the file you can check [the official Ryu GitHub repository](https://github.com/osrg/ryu).\n\nAssuming you want to edit this file using your favorite text editor (definitely vi), you would enter\n\n```bash\n$ sudo vi /usr/local/lib/python2.7/dist-packages/ryu/app/simple_switch_13.py\n```\n\nAlternatively, you can also make a copy of this file to your home directory and link this copy to the controller\n\n```bash\n$ cd\n$ cp /usr/local/lib/python2.7/dist-packages/ryu/app/simple_switch_13.py ~/sw.py\n$ sudo ryu-manager sw.py\n```\n\n**Keep track of which copy of the `simple_switch_13.py` you are editing since this is one of the files you will have to hand in.**\n\n**NOTE** \n\nAll the files in the `ryu` folder are created by the `root` user. When you make a copy with `sudo`, the owner will remain the `root` user. In this case, the copy cannot be edited by the user `nwen302`. It is better to change the file owner with the following command:\n\n```bash\n$sudo chown nwen302:nwen302 sw.py\n```\n\nBut keep in mind that you will still need to run Ryu and Mininet using sudo.\n\n### Understanding `simple_switch_13.py`\n---\n\nThe simple switch keeps track of where the host with each MAC address is located and accordingly sends packets towards the destination and not flood all ports.\n\nPacket logic:\n\n```bash\nCreate a table called mac_to_port;\nif { packet_in to switch }  {    \n    Parse packet to reveal src and dst MAC addr;\n    Store in the dictionary the mapping between \n        src_mac and the in_port;\n    Lookup dst_mac in mac_to_port dict of switch \n        s1 to find next hop;\n    if { next hop is found } { \n        create flow_mod;\n        send;\n     } else { \n         flood all ports != in_port;\n    }\n}\n```\n\nA more detailed description of the `simple_switch_13.py` file can be found in the [RYU SDN FRAMEWORK for OpenFlow 1.3](https://osrg.github.io/ryu-book/en/Ryubook.pdf), in *Chapter 4*. \n\n## Requirements\n---\n\n### Key Task 1\n---\n\nModify `simple_switch_13.py` to include logic to block traffic between host 2 and host 3.\n\n### Key Task 2\n---\n\nExtend `simple_switch_13.py` to count all traffic going to and originating from host 1.\n\n### Key Task 3\n---\n\nExtend `simple_switch_13.py` to combine Task 1 and Task 2 functionalities. Keep track of all traffic (count the number of packets) originating from each host. If the counter exceeds a specific number, block all the traffic originating from this host for 24 hours. The maximum packet count number should be configured through `MAX_COUNT` variable.\n\n\u003c!--\nCreate a rule in `simple_switch_13.py` that routes messages to the controller for topology maintenance.\n\n*HINT*: Ryu’s topology viewer uses [[http://en.wikipedia.org/wiki/Link_Layer_Discovery_Protocol][LLDP]] to visualize routes, you will need to create a simple database application to maintain routes and then trap LLDP messages to update the database. You may assume a single database and do not need to address any concurrency issues.\n\nPlease see the attachment \"Topology_Discovery_with_Ryu.pdf\" for more information.\n\n--\u003e\n\n\n## What to hand in\n---\n\n   * All relevant code. i.e. the modified `simple_switch_13.py` (if you haven't changed its name) and whatever new Python files you've created.\n      *   If you have \"borrowed\" code from on-line resources and/or other sources, say so in your code comments.\n   * A report (max 2,000 words, pictures are always good) describing your development process.\n      *   State which of the three tasks listed above you believe you have successfully implemented.\n      *   Reflect on what you found easy, what caused you problems and how you overcame those issues.\n      *   If you believe you have a particularly sophisticated or original implementation, state why you think this is the case.\n      *   Carefully document all investigatory steps taken. If you found several ways to do something, stare why you chose the one you used.\n      *   Take care to document all testing steps take.\n\n## Grading scheme\n---\n\nThe following three aspects will be assessed:\n\n1. (60%) **Does it work?**\n      *   Task 1 (20%)\n      *   Task 2 (20%)\n      *   Task 3 (20%)\n2. (10%) **Is the code well written?**\n      *   Marks awarded for:\n            * Clarity - Is it obvious where your changes are? Are they well commented?\n            * Modularity - Remember Pythons ability to include and extend existing classes.\n            * Giving credit to on-line resources you “borrowed\" code segments from (if any).\n            * Evidence of original work (i.e. not simply having copied exisiting code).\n      *   Marks deducted for:\n           * Not giving credit to on-line resources you “borrowed\" code segments from.\n3. (30%) **Did you understand what you were doing?**\n      *   The code partially show this, but the report is where you convince me.\n      *   Marks awarded for:\n           * Clearly stating which tasks were achieved and which were not (and why).\n           * Detail on how each task was tackled, what alternatives were considered and why a particular solution was chosen.\n           * Description of development process.\n           * Details of testing methodology - VERY IMPORTANT, provide lots of detail here, this is where the marks are!\n           * Observations of what was easy, what was hard, where problems arose, how they were overcome.\n           * Reflection of how you might improve or extend your solution.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrnpkr%2Fsdn-lab-assignment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrnpkr%2Fsdn-lab-assignment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrnpkr%2Fsdn-lab-assignment/lists"}