def link_ports(self, nffg, flowid1, flowid2, port1, port2): if not nffg.existPort(port1): raise Exception("Port with id:"+port1+" is invalid") if not nffg.existPort(port2): raise Exception("Port with id:"+port2+" is invalid") nffg.addFlowRule(FlowRule(flowid1, 1, Match(port1), [Action(port2)])) nffg.addFlowRule(FlowRule(flowid2, 1, Match(port2), [Action(port1)]))
def set_default(self, nffg, out_port): flowrules = nffg.getFlowRulesStartingWith("LB_DEFAULT_") for flowrule in flowrules: logging.debug("removing flow rule with name '%s' " % (flowrule.id)) nffg.removeFlowRule(flowrule.id) logging.debug("setting default flow rule : port_in=%s -> %s " % (self.port_in, out_port)) nffg.addFlowRule( FlowRule("LB_DEFAULT_" + str(self.counter), 1, Match(port_in=self.port_in), [Action(out_port)])) logging.debug("setting reply flow rule : port_in=%s -> %s " % (out_port, self.port_in)) nffg.addFlowRule( FlowRule("LB_REPLY_" + str(self.counter), 2, Match(port_in=out_port), [Action(self.port_in)])) self.counter = self.counter + 1
def addEndpointsCoupleAndFlowrules(self, endpoint_id, flowrule_priority=10): endpoint_1 = EndPoint(_id = str(endpoint_id)+"_1") endpoint_2 = EndPoint(_id = str(endpoint_id)+"_2") flowrule_1 = FlowRule() flowrule_1.id = endpoint_1.id flowrule_1.priority = flowrule_priority flowrule_1.match = Match(port_in="endpoint:"+endpoint_1.id) flowrule_1.actions.append(Action(output="endpoint:"+endpoint_2.id)) flowrule_2 = FlowRule() flowrule_2.id = endpoint_2.id flowrule_2.priority = flowrule_priority flowrule_2.match = Match(port_in="endpoint:"+endpoint_2.id) flowrule_2.actions.append(Action(output="endpoint:"+endpoint_1.id)) self.nffg.addEndPoint(endpoint_1) self.nffg.addEndPoint(endpoint_2) self.nffg.addFlowRule(flowrule_1) self.nffg.addFlowRule(flowrule_2)
def add_balance(self, nffg, host_mac, out_port): flow_rule_name = "LB_" + host_mac.replace(":", "") logging.info( "setting flow rule : port_in=%s,source_mac=%s,dest_mac=%s -> %s " % (self.port_in, host_mac, self.mac, out_port)) if self.exist_balance(nffg, host_mac): raise Exception( "Already exist a load-balancing rule for source_mac=%s!" % host_mac) nffg.addFlowRule( FlowRule( flow_rule_name, 2, Match(port_in=self.port_in, source_mac=host_mac, dest_mac=self.mac), [Action(out_port)]))
def getNFFG(self, session_id): session = get_session() session_ref = session.query(GraphSessionModel).filter_by(session_id=session_id).one() # [ NF-FG ] nffg = NF_FG() nffg.id = session_ref.graph_id nffg.name = session_ref.graph_name nffg.description = session_ref.description # [ ENDPOINTs ] end_points_ref = session.query(EndpointModel).filter_by(session_id=session_id).all() for end_point_ref in end_points_ref: # Add endpoint to NFFG end_point = EndPoint(_id=end_point_ref.graph_endpoint_id, name=end_point_ref.name, _type=end_point_ref.type, db_id=end_point_ref.id) nffg.addEndPoint(end_point) # End_point resource end_point_resorces_ref = session.query(EndpointResourceModel).filter_by(endpoint_id=end_point_ref.id).all() for end_point_resorce_ref in end_point_resorces_ref: if end_point_resorce_ref.resource_type == 'port': try: port_ref = session.query(PortModel).filter_by(id=end_point_resorce_ref.resource_id).one() except NoResultFound: port_ref = None logging.debug("Port not found for endpoint "+end_point_ref.graph_endpoint_id) if port_ref is not None: end_point.switch_id = port_ref.switch_id end_point.interface = port_ref.graph_port_id end_point.vlan_id = port_ref.vlan_id # [ FLOW RULEs ] flow_rules_ref = session.query(FlowRuleModel).filter_by(session_id=session_id).all() for flow_rule_ref in flow_rules_ref: if flow_rule_ref.type is not None: # None or 'external' continue # Add flow rule to NFFG flow_rule = FlowRule(_id=flow_rule_ref.graph_flow_rule_id, internal_id=flow_rule_ref.internal_id, priority=int(flow_rule_ref.priority), description=flow_rule_ref.description, db_id=flow_rule_ref.id) nffg.addFlowRule(flow_rule) # [ MATCH ] try: match_ref = session.query(MatchModel).filter_by(flow_rule_id=flow_rule_ref.id).one() except NoResultFound: match_ref = None logging.debug("Found flowrule without a match") if match_ref is not None: # Retrieve endpoint data port_in = None if match_ref.port_in_type == 'endpoint': end_point_ref = session.query(EndpointModel).filter_by(id=match_ref.port_in).first() port_in = 'endpoint:'+end_point_ref.graph_endpoint_id # Add match to this flow rule match = Match(port_in=port_in, ether_type=match_ref.ether_type, vlan_id=match_ref.vlan_id, vlan_priority=match_ref.vlan_priority, source_mac=match_ref.source_mac, dest_mac=match_ref.dest_mac, source_ip=match_ref.source_ip, dest_ip=match_ref.dest_ip, tos_bits=match_ref.tos_bits, source_port=match_ref.source_port, dest_port=match_ref.dest_port, protocol=match_ref.protocol, db_id=match_ref.id) flow_rule.match = match # [ ACTIONs ] actions_ref = session.query(ActionModel).filter_by(flow_rule_id=flow_rule_ref.id).all() if len(actions_ref)==0: logging.debug("Found flowrule without actions") for action_ref in actions_ref: output_to_port = None # Retrieve endpoint data if action_ref.output_type == 'endpoint': end_point_ref = session.query(EndpointModel).filter_by(id = action_ref.output_to_port).first() output_to_port = action_ref.output_type+':'+end_point_ref.graph_endpoint_id # Add action to this flow rule action = Action(output=output_to_port, controller=action_ref.output_to_controller, drop=action_ref._drop, set_vlan_id=action_ref.set_vlan_id, set_vlan_priority=action_ref.set_vlan_priority, push_vlan=action_ref.push_vlan, pop_vlan=action_ref.pop_vlan, set_ethernet_src_address=action_ref.set_ethernet_src_address, set_ethernet_dst_address=action_ref.set_ethernet_dst_address, set_ip_src_address=action_ref.set_ip_src_address, set_ip_dst_address=action_ref.set_ip_dst_address, set_ip_tos=action_ref.set_ip_tos, set_l4_src_port=action_ref.set_l4_src_port, set_l4_dst_port=action_ref.set_l4_dst_port, output_to_queue=action_ref.output_to_queue, db_id=action_ref.id) flow_rule.actions.append(action) return nffg