-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyretic_switch.py
62 lines (54 loc) · 3.4 KB
/
pyretic_switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# CS 6250 Summer 2020 - Project 4 - SDN Firewall
# build atlas-v13
################################################################################
# The Pyretic Project #
# frenetic-lang.org/pyretic #
# author: Joshua Reich ([email protected]) #
################################################################################
# Licensed to the Pyretic Project by one or more contributors. See the #
# NOTICES file distributed with this work for additional information #
# regarding copyright and ownership. The Pyretic Project licenses this #
# file to you under the following license. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided the following conditions are met: #
# - Redistributions of source code must retain the above copyright #
# notice, this list of conditions and the following disclaimer. #
# - Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in #
# the documentation or other materials provided with the distribution. #
# - The names of the copyright holds and contributors may not be used to #
# endorse or promote products derived from this work without specific #
# prior written permission. #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT #
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the #
# LICENSE file distributed with this work for specific language governing #
# permissions and limitations under the License. #
################################################################################
from pyretic.lib.corelib import *
from pyretic.lib.std import *
from pyretic.lib.query import *
class ActLikeSwitch(DynamicPolicy):
def __init__(self):
super(ActLikeSwitch, self).__init__()
# Set up the initial forwarding behavior for your mac learning switch to flood
# all packets
self.forward = flood()
# Set up a query that will receive new incoming packets
self.query = packets(limit=1,group_by=['srcmac','switch'])
# Set the initial internal policy value (each dynamic policy has a member 'policy'
# when this member is assigned, the dynamic policy updates itself)
self.policy = self.forward + self.query
self.query.register_callback(self.learn_from_a_packet)
def learn_from_a_packet(self, pkt):
# Set the forwarding policy
self.forward = if_(match(dstmac=pkt['srcmac'],
switch=pkt['switch']), fwd(pkt['inport']),
self.forward) # hint use 'match', '&', 'if_', and 'fwd'
# Update the policy
self.policy = self.forward + self.query # hint you've already written this
print self.policy
def main():
return ActLikeSwitch()