| 1 |  |  | """Main module of amlight/sdntrace_cp Kytos Network Application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | Run tracepaths on OpenFlow in the Control Plane | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | import ipaddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | from datetime import datetime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 1 |  | from flask import jsonify, request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  | from kytos.core import KytosNApp, log, rest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 1 |  | from napps.amlight.sdntrace_cp import settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 1 |  | from napps.amlight.sdntrace_cp.automate import Automate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 1 |  | from napps.amlight.sdntrace_cp.utils import (convert_entries, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |                                              convert_list_entries, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |                                              find_endpoint, get_stored_flows, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |                                              prepare_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  | class Main(KytosNApp): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     """Main class of amlight/sdntrace_cp NApp. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     This application gets the list of flows from the switches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     and uses it to trace paths without using the data plane. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |     def setup(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         """Replace the '__init__' method for the KytosNApp subclass. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         The setup method is automatically called by the controller when your | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         application is loaded. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         log.info("Starting Kytos SDNTrace CP App!") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         self.traces = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         self.last_id = 30000 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         self.automate = Automate(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         self.automate.schedule_traces() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         self.automate.schedule_important_traces() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |     def execute(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         """This method is executed right after the setup method execution. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         You can also use this method in loop mode if you add to the above setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         method a line like the following example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             self.execute_as_loop(30)  # 30-second interval. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |     def shutdown(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         """This method is executed when your napp is unloaded. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         If you have some cleanup procedure, insert it here. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self.automate.unschedule_ids() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self.automate.sheduler_shutdown(wait=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |     @rest('/trace', methods=['PUT']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     def trace(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         """Trace a path.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         result = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         entries = request.get_json() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         entries = convert_entries(entries) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         if not entries: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return "Bad request", 400 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         stored_flows = get_stored_flows() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         result = self.tracepath(entries, stored_flows) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         return jsonify(prepare_json(result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |     @rest('/traces', methods=['PUT']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |     def get_traces(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         """For bulk requests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         entries = request.get_json() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         entries = convert_list_entries(entries) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         stored_flows = get_stored_flows() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         results = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         for entry in entries: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             results.append(self.tracepath(entry, stored_flows)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         return jsonify(prepare_json(results)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |     def tracepath(self, entries, stored_flows): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         """Trace a path for a packet represented by entries.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         # pylint: disable=too-many-branches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         self.last_id += 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         trace_id = self.last_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         trace_result = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         trace_type = 'starting' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         do_trace = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         while do_trace: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             if 'dpid' not in entries or 'in_port' not in entries: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |                 break | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             trace_step = {'in': {'dpid': entries['dpid'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                                  'port': entries['in_port'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                                  'time': str(datetime.now()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                                  'type': trace_type}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             if 'dl_vlan' in entries: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |                 trace_step['in'].update({'vlan': entries['dl_vlan'][-1]}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             switch = self.controller.get_switch_by_dpid(entries['dpid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |             if not switch: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |                 trace_step['in']['type'] = 'last' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |                 trace_result.append(trace_step) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |                 break | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |             result = self.trace_step(switch, entries, stored_flows) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             if result: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |                 out = {'port': result['out_port']} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                 if 'dl_vlan' in result['entries']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                     out.update({'vlan': result['entries']['dl_vlan'][-1]}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |                 trace_step.update({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     'out': out | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |                 if 'dpid' in result: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |                     next_step = {'dpid': result['dpid'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                                  'port': result['in_port']} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |                     entries = result['entries'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |                     entries['dpid'] = result['dpid'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |                     entries['in_port'] = result['in_port'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |                     if self.has_loop(next_step, trace_result): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |                         trace_step['in']['type'] = 'loop' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |                         do_trace = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |                         trace_type = 'intermediary' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |                     trace_step['in']['type'] = 'last' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |                     do_trace = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |                 trace_step['in']['type'] = 'incomplete' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |                 do_trace = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |             if 'out' in trace_step and trace_step['out']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |                 if self.check_loop_trace_step(trace_step, trace_result): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |                     do_trace = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             trace_result.append(trace_step) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |         self.traces.update({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             trace_id: trace_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         return trace_result | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 | 1 |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 1 |  |     def check_loop_trace_step(trace_step, trace_result): | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         """Check if there is a loop in the trace and add the step.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         # outgoing interface is the same as the input interface | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 1 |  |         if not trace_result and \ | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |                 trace_step['in']['type'] == 'last' and \ | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |                 trace_step['in']['port'] == trace_step['out']['port']: | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 1 |  |             trace_step['in']['type'] = 'loop' | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 1 |  |             return True | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 1 |  |         if trace_result and \ | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |                 trace_result[0]['in']['dpid'] == trace_step['in']['dpid'] and \ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |                 trace_result[0]['in']['port'] == trace_step['out']['port']: | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 1 |  |             trace_step['in']['type'] = 'loop' | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 1 |  |             return True | 
            
                                                                        
                            
            
                                    
            
            
                | 152 | 1 |  |         return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |     def has_loop(trace_step, trace_result): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         """Check if there is a loop in the trace result.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |         for trace in trace_result: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |             if trace['in']['dpid'] == trace_step['dpid'] and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                             trace['in']['port'] == trace_step['port']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |                 return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |         return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |     def trace_step(self, switch, entries, stored_flows): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         """Perform a trace step. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         Match the given fields against the switch's list of flows.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |         flow, entries, port = self.match_and_apply( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                                                     switch, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                                                     entries, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                                                     stored_flows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                                                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |         if not flow or not port: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 1 |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 1 |  |         endpoint = find_endpoint(switch, port) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |         if endpoint is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 1 |  |             return {'out_port': port, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                     'entries': entries} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 1 |  |         return {'dpid': endpoint.switch.dpid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                 'in_port': endpoint.port_number, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 'out_port': port, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 'entries': entries} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  |     def update_circuits(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         """Update the list of circuits after a flow change.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         # pylint: disable=unused-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 1 |  |         if settings.FIND_CIRCUITS_IN_FLOWS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  |             self.automate.find_circuits() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 1 |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 1 |  |     def do_match(cls, flow, args): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         """Match a packet against this flow (OF1.3).""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         # pylint: disable=consider-using-dict-items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  |         if ('match' not in flow['flow']) or (len(flow['flow']['match']) == 0): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 1 |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 1 |  |         for name in flow['flow']['match']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 1 |  |             field_flow = flow['flow']['match'][name] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 1 |  |             if name not in args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |                 return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 1 |  |             if name == 'dl_vlan': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 1 |  |                 field = args[name][-1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |             else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 1 |  |                 field = args[name] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 1 |  |             if name not in ('ipv4_src', 'ipv4_dst', 'ipv6_src', 'ipv6_dst'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 1 |  |                 if field_flow != field: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 1 |  |                     return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |                 packet_ip = int(ipaddress.ip_address(field)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |                 ip_addr = flow['flow']['match'][name] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |                 if packet_ip & ip_addr.netmask != ip_addr.address: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |                     return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 1 |  |         return flow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 | 1 |  |     def match_flows(self, switch, args, stored_flows, many=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         # pylint: disable=bad-staticmethod-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         Match the packet in request against the stored flows from flow_manager. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         Try the match with each flow, in other. If many is True, tries the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         match with all flows, if False, tries until the first match. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         :param args: packet data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         :param many: Boolean, indicating whether to continue after matching the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |                 first flow or not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         :return: If many, the list of matched flows, or the matched flow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 1 |  |         if switch.dpid not in stored_flows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 1 |  |         response = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 1 |  |         if switch.dpid not in stored_flows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 | 1 |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 | 1 |  |             for flow in stored_flows[switch.dpid]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 | 1 |  |                 match = Main.do_match(flow, args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 | 1 |  |                 if match: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 | 1 |  |                     if many: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |                         response.append(match) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 1 |  |                         response = match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 | 1 |  |                         break | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         except AttributeError: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 | 1 |  |         if not many and isinstance(response, list): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 | 1 |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 1 |  |         return response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |     # pylint: disable=redefined-outer-name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 1 |  |     def match_and_apply(self, switch, args, stored_flows): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         # pylint: disable=bad-staticmethod-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         """Match flows and apply actions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         Match given packet (in args) against | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         the stored flows (from flow_manager) and, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         if a match flow is found, apply its actions.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 | 1 |  |         flow = self.match_flows(switch, args, stored_flows, False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 | 1 |  |         port = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 | 1 |  |         actions = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         # pylint: disable=too-many-nested-blocks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 1 |  |         if not flow or switch.ofp_version != '0x04': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 1 |  |             return flow, args, port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 | 1 |  |         if 'actions' in flow['flow']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 | 1 |  |             actions = flow['flow']['actions'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 1 |  |         for action in actions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 | 1 |  |             action_type = action['action_type'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 | 1 |  |             if action_type == 'output': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 | 1 |  |                 port = action['port'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 | 1 |  |             if action_type == 'push_vlan': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 | 1 |  |                 if 'dl_vlan' not in args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                     args['dl_vlan'] = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 | 1 |  |                 args['dl_vlan'].append(0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 | 1 |  |             if action_type == 'pop_vlan': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 | 1 |  |                 if 'dl_vlan' in args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 | 1 |  |                     args['dl_vlan'].pop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 | 1 |  |                     if len(args['dl_vlan']) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 | 1 |  |                         del args['dl_vlan'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 | 1 |  |             if action_type == 'set_vlan': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 | 1 |  |                 args['dl_vlan'][-1] = action['vlan_id'] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 277 |  |  |         return flow, args, port | 
            
                                                        
            
                                    
            
            
                | 278 |  |  |  |