Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | Simple PON Simulator which would not be needed if openvswitch could do |
| 19 | 802.1ad (QinQ), which it cannot (the reason is beyond me), or if CPQD could |
| 20 | handle 0-tagged packets (no comment). |
| 21 | """ |
| 22 | import structlog |
| 23 | from scapy.layers.inet import IP, UDP |
| 24 | from scapy.layers.l2 import Ether, Dot1Q |
| 25 | from scapy.packet import Packet |
| 26 | |
| 27 | from common.frameio.frameio import hexify |
| 28 | from voltha.protos import third_party |
| 29 | from voltha.core.flow_decomposer import * |
| 30 | _ = third_party |
| 31 | |
| 32 | |
| 33 | def ipv4int2str(ipv4int): |
| 34 | return '{}.{}.{}.{}'.format( |
| 35 | (ipv4int >> 24) & 0xff, |
| 36 | (ipv4int >> 16) & 0xff, |
| 37 | (ipv4int >> 8) & 0xff, |
| 38 | ipv4int & 0xff |
| 39 | ) |
| 40 | |
| 41 | |
| 42 | class SimDevice(object): |
| 43 | |
| 44 | def __init__(self, name, logical_port_no): |
| 45 | self.name = name |
| 46 | self.logical_port_no = logical_port_no |
| 47 | self.links = dict() |
| 48 | self.flows = list() |
| 49 | self.log = structlog.get_logger(name=name, |
| 50 | logical_port_no=logical_port_no) |
| 51 | |
| 52 | def link(self, port, egress_fun): |
| 53 | self.links.setdefault(port, []).append(egress_fun) |
| 54 | |
| 55 | def ingress(self, port, frame): |
| 56 | self.log.debug('ingress', ingress_port=port) |
| 57 | outcome = self.process_frame(port, frame) |
| 58 | if outcome is not None: |
| 59 | egress_port, egress_frame = outcome |
| 60 | forwarded = 0 |
| 61 | links = self.links.get(egress_port) |
| 62 | if links is not None: |
| 63 | for fun in links: |
| 64 | forwarded += 1 |
| 65 | self.log.debug('forwarding', egress_port=egress_port) |
| 66 | fun(egress_port, egress_frame) |
| 67 | if not forwarded: |
| 68 | self.log.debug('no-one-to-forward-to', egress_port=egress_port) |
| 69 | else: |
| 70 | self.log.debug('dropped') |
| 71 | |
| 72 | def install_flows(self, flows): |
| 73 | # store flows in precedence order so we can roll down on frame arrival |
| 74 | self.flows = sorted(flows, key=lambda fm: fm.priority, reverse=True) |
| 75 | |
| 76 | def process_frame(self, ingress_port, ingress_frame): |
| 77 | for flow in self.flows: |
| 78 | if self.is_match(flow, ingress_port, ingress_frame): |
| 79 | egress_port, egress_frame = self.process_actions( |
| 80 | flow, ingress_frame) |
| 81 | return egress_port, egress_frame |
| 82 | return None |
| 83 | |
| 84 | @staticmethod |
| 85 | def is_match(flow, ingress_port, frame): |
| 86 | |
| 87 | def get_non_shim_ether_type(f): |
| 88 | if f.haslayer(Dot1Q): |
| 89 | f = f.getlayer(Dot1Q) |
| 90 | return f.type |
| 91 | |
| 92 | def get_vlan_pcp(f): |
| 93 | if f.haslayer(Dot1Q): |
| 94 | return f.getlayer(Dot1Q).prio |
| 95 | |
| 96 | def get_ip_proto(f): |
| 97 | if f.haslayer(IP): |
| 98 | return f.getlayer(IP).proto |
| 99 | |
| 100 | def get_ipv4_dst(f): |
| 101 | if f.haslayer(IP): |
| 102 | return f.getlayer(IP).dst |
| 103 | |
| 104 | def get_udp_dst(f): |
| 105 | if f.haslayer(UDP): |
| 106 | return f.getlayer(UDP).dport |
| 107 | |
| 108 | for field in get_ofb_fields(flow): |
| 109 | |
| 110 | if field.type == IN_PORT: |
| 111 | if field.port != ingress_port: |
| 112 | return False |
| 113 | |
| 114 | elif field.type == ETH_TYPE: |
| 115 | if field.eth_type != get_non_shim_ether_type(frame): |
| 116 | return False |
| 117 | |
| 118 | elif field.type == IP_PROTO: |
| 119 | if field.ip_proto != get_ip_proto(frame): |
| 120 | return False |
| 121 | |
| 122 | elif field.type == VLAN_VID: |
| 123 | expected_vlan = field.vlan_vid |
| 124 | tagged = frame.haslayer(Dot1Q) |
| 125 | if bool(expected_vlan & 4096) != bool(tagged): |
| 126 | return False |
| 127 | if tagged: |
| 128 | actual_vid = frame.getlayer(Dot1Q).vlan |
| 129 | if actual_vid != expected_vlan & 4095: |
| 130 | return False |
| 131 | |
| 132 | elif field.type == VLAN_PCP: |
| 133 | if field.vlan_pcp != get_vlan_pcp(frame): |
| 134 | return False |
| 135 | |
| 136 | elif field.type == IPV4_DST: |
| 137 | if ipv4int2str(field.ipv4_dst) != get_ipv4_dst(frame): |
| 138 | return False |
| 139 | |
| 140 | elif field.type == UDP_DST: |
| 141 | if field.udsp_dst != get_udp_dst(frame): |
| 142 | return False |
| 143 | |
| 144 | else: |
| 145 | raise NotImplementedError('field.type=%d' % field.type) |
| 146 | |
| 147 | return True |
| 148 | |
| 149 | @staticmethod |
| 150 | def process_actions(flow, frame): |
| 151 | egress_port = None |
| 152 | for action in get_actions(flow): |
| 153 | |
| 154 | if action.type == OUTPUT: |
| 155 | egress_port = action.output.port |
| 156 | |
| 157 | elif action.type == POP_VLAN: |
| 158 | if frame.haslayer(Dot1Q): |
| 159 | shim = frame.getlayer(Dot1Q) |
| 160 | frame = Ether( |
| 161 | src=frame.src, |
| 162 | dst=frame.dst, |
| 163 | type=shim.type) / shim.payload |
| 164 | |
| 165 | elif action.type == PUSH_VLAN: |
| 166 | frame = ( |
| 167 | Ether(src=frame.src, dst=frame.dst, |
| 168 | type=action.push.ethertype) / |
| 169 | Dot1Q(type=frame.type) / |
| 170 | frame.payload |
| 171 | ) |
| 172 | |
| 173 | elif action.type == SET_FIELD: |
| 174 | assert (action.set_field.field.oxm_class == |
| 175 | ofp.OFPXMC_OPENFLOW_BASIC) |
| 176 | field = action.set_field.field.ofb_field |
| 177 | |
| 178 | if field.type == VLAN_VID: |
| 179 | shim = frame.getlayer(Dot1Q) |
| 180 | shim.vlan = field.vlan_vid & 4095 |
| 181 | |
| 182 | elif field.type == VLAN_PCP: |
| 183 | shim = frame.getlayer(Dot1Q) |
| 184 | shim.prio = field.vlan_pcp |
| 185 | |
| 186 | else: |
| 187 | raise NotImplementedError('set_field.field.type=%d' |
| 188 | % field.type) |
| 189 | |
| 190 | else: |
| 191 | raise NotImplementedError('action.type=%d' % action.type) |
| 192 | |
| 193 | return egress_port, frame |
| 194 | |
| 195 | |
| 196 | class PonSim(object): |
| 197 | |
| 198 | def __init__(self, onus, egress_fun): |
| 199 | self.egress_fun = egress_fun |
| 200 | |
| 201 | # Create OLT and hook NNI port up for egress |
| 202 | self.olt = SimDevice('olt', 0) |
| 203 | self.olt.link(2, lambda _, frame: self.egress_fun(0, frame)) |
| 204 | self.devices = dict() |
| 205 | self.devices[0] = self.olt |
| 206 | |
| 207 | # Create ONUs of the requested number and hook them up with OLT |
| 208 | # and with egress fun |
| 209 | def mk_egress_fun(port_no): |
| 210 | return lambda _, frame: self.egress_fun(port_no, frame) |
| 211 | |
| 212 | def mk_onu_ingress(onu): |
| 213 | return lambda _, frame: onu.ingress(1, frame) |
| 214 | |
| 215 | for i in range(onus): |
| 216 | port_no = 128 + i |
| 217 | onu = SimDevice('onu%d' % i, port_no) |
| 218 | onu.link(1, lambda _, frame: self.olt.ingress(1, frame)) |
| 219 | onu.link(2, mk_egress_fun(port_no)) |
| 220 | self.olt.link(1, mk_onu_ingress(onu)) |
| 221 | self.devices[port_no] = onu |
| 222 | |
| 223 | def get_ports(self): |
| 224 | return sorted(self.devices.keys()) |
| 225 | |
| 226 | def olt_install_flows(self, flows): |
| 227 | self.olt.install_flows(flows) |
| 228 | |
| 229 | def onu_install_flows(self, onu_port, flows): |
| 230 | self.devices[onu_port].install_flows(flows) |
| 231 | |
| 232 | def ingress(self, port, frame): |
| 233 | if not isinstance(frame, Packet): |
| 234 | frame = Ether(frame) |
| 235 | self.devices[port].ingress(2, frame) |
| 236 | |