Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.

Commit 34ff855

Browse files
author
Joshua Reich
committed
OF_inportize: fix bug and clean code
1 parent 027dd50 commit 34ff855

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

pyretic/core/runtime.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def concretize_action(a):
534534
crs = filter(lambda cr: not cr is None,crs)
535535
return Classifier(crs)
536536

537-
def portize(classifier,switch_to_attrs):
537+
def OF_inportize(classifier):
538538
"""
539539
Specialize classifier to ensure that packets to be forwarded
540540
out the inport on which they arrived are handled correctly.
@@ -545,25 +545,42 @@ def portize(classifier,switch_to_attrs):
545545
:rtype: Classifier
546546
"""
547547
import copy
548+
def specialize_actions(actions,outport):
549+
new_actions = copy.deepcopy(actions)
550+
for action in new_actions:
551+
try:
552+
if action['outport'] == outport:
553+
action['outport'] = OFPP_IN_PORT
554+
except:
555+
raise TypeError # INVARIANT: every set of actions must go out a port
556+
# this may not hold when we move to OF 1.3
557+
return new_actions
558+
548559
specialized_rules = []
549560
for rule in classifier.rules:
561+
phys_actions = filter(lambda a: (a['outport'] != OFPP_CONTROLLER
562+
and a['outport'] != OFPP_IN_PORT),
563+
rule.actions)
564+
outports_used = map(lambda a: a['outport'], phys_actions)
550565
if not 'inport' in rule.match:
551-
phys_actions = filter(lambda a: a['outport'] != OFPP_CONTROLLER and a['outport'] != OFPP_IN_PORT,rule.actions)
552-
outports_used = map(lambda a: a['outport'], phys_actions)
566+
# Add a modified rule for each of the outports_used
553567
switch = rule.match['switch']
554568
for outport in outports_used:
555569
new_match = copy.deepcopy(rule.match)
556570
new_match['inport'] = outport
557-
new_actions = copy.deepcopy(rule.actions)
558-
for action in new_actions:
559-
try:
560-
if action['outport'] == outport:
561-
action['outport'] = OFPP_IN_PORT
562-
except:
563-
raise TypeError # INVARIANT: every set of actions must go out a port
564-
# this may not hold when we move to OF 1.3
571+
new_actions = specialize_actions(rule.actions,outport)
565572
specialized_rules.append(Rule(new_match,new_actions))
566-
specialized_rules.append(rule)
573+
# And a default rule for any inport outside the set of outports_used
574+
specialized_rules.append(rule)
575+
else:
576+
if rule.match['inport'] in outports_used:
577+
# Modify the set of actions
578+
new_actions = specialize_actions(rule.actions,rule.match['inport'])
579+
specialized_rules.append(Rule(rule.match,new_actions))
580+
else:
581+
# Leave as before
582+
specialized_rules.append(rule)
583+
567584
return Classifier(specialized_rules)
568585

569586
def prioritize(classifier):
@@ -601,7 +618,7 @@ def nuclear_install(classifier):
601618
switches = switch_to_attrs.keys()
602619
classifier = switchify(classifier,switches)
603620
classifier = concretize(classifier)
604-
classifier = portize(classifier,switch_to_attrs)
621+
classifier = OF_inportize(classifier)
605622
new_rules = prioritize(classifier)
606623

607624
for s in switches:
@@ -643,7 +660,7 @@ def install_diff_rules(classifier):
643660
switches = switch_to_attrs.keys()
644661
classifier = switchify(classifier,switches)
645662
classifier = concretize(classifier)
646-
classifier = portize(classifier,switch_to_attrs)
663+
classifier = OF_inportize(classifier)
647664
new_rules = prioritize(classifier)
648665

649666
# calculate diff

0 commit comments

Comments
 (0)