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 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 27 | from voltha.protos import third_party |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 28 | from voltha.protos.ponsim_pb2 import PonSimMetrics, PonSimPortMetrics, \ |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 29 | PonSimPacketCounter |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 30 | from voltha.core.flow_decomposer import * |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 31 | from twisted.internet.task import LoopingCall |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 32 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 33 | _ = third_party |
| 34 | |
| 35 | |
| 36 | def ipv4int2str(ipv4int): |
| 37 | return '{}.{}.{}.{}'.format( |
| 38 | (ipv4int >> 24) & 0xff, |
| 39 | (ipv4int >> 16) & 0xff, |
| 40 | (ipv4int >> 8) & 0xff, |
| 41 | ipv4int & 0xff |
| 42 | ) |
| 43 | |
| 44 | |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 45 | class _FlowMatchMask(object): |
| 46 | """ |
| 47 | Enum of mask values based on flow match priority. For instance, a port |
| 48 | match has higher priority when match that a UDP match. |
| 49 | """ |
| 50 | UDP_DST = 1 |
| 51 | UDP_SRC = 2 |
| 52 | IPV4_DST = 4 |
| 53 | VLAN_PCP = 8 |
| 54 | VLAN_VID = 16 |
| 55 | IP_PROTO = 34 |
| 56 | ETH_TYPE = 64 |
| 57 | IN_PORT = 128 |
| 58 | |
| 59 | |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 60 | class FrameIOCounter(object): |
| 61 | class SingleFrameCounter(object): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 62 | def __init__(self, name, min, max): |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 63 | # Currently there are 2 values, one for the PON interface (port 1) |
| 64 | # and one for the Network Interface (port 2). This can be extended if |
| 65 | # the virtual devices extend the number of ports. |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 66 | self.value = [0, 0] # {PON,NI} |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 67 | self.name = name |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 68 | self.min = min |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 69 | self.max = max |
| 70 | |
| 71 | def __init__(self, device): |
| 72 | self.device = device |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 73 | self.tx_counters = dict( |
| 74 | tx_64=self.SingleFrameCounter("tx_64", 1, 64), |
| 75 | tx_65_127=self.SingleFrameCounter("tx_65_127", 65, 127), |
| 76 | tx_128_255=self.SingleFrameCounter("tx_128_255", 128, 255), |
| 77 | tx_256_511=self.SingleFrameCounter("tx_256_511", 256, 511), |
| 78 | tx_512_1023=self.SingleFrameCounter("tx_512_1023", 512, 1024), |
| 79 | tx_1024_1518=self.SingleFrameCounter("tx_1024_1518", 1024, 1518), |
| 80 | tx_1519_9k=self.SingleFrameCounter("tx_1519_9k", 1519, 9216), |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 81 | ) |
| 82 | self.rx_counters = dict( |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 83 | rx_64=self.SingleFrameCounter("rx_64", 1, 64), |
| 84 | rx_65_127=self.SingleFrameCounter("rx_65_127", 65, 127), |
| 85 | rx_128_255=self.SingleFrameCounter("rx_128_255", 128, 255), |
| 86 | rx_256_511=self.SingleFrameCounter("rx_256_511", 256, 511), |
| 87 | rx_512_1023=self.SingleFrameCounter("rx_512_1023", 512, 1024), |
| 88 | rx_1024_1518=self.SingleFrameCounter("rx_1024_1518", 1024, 1518), |
| 89 | rx_1519_9k=self.SingleFrameCounter("rx_1519_9k", 1519, 9216) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 90 | ) |
| 91 | |
| 92 | def count_rx_frame(self, port, size): |
| 93 | log.info("counting-rx-frame", size=size, port=port) |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 94 | for k, v in self.rx_counters.iteritems(): |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 95 | if size >= v.min and size <= v.max: |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 96 | self.rx_counters[k].value[port - 1] += 1 |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 97 | return |
| 98 | log.warn("unsupported-packet-size", size=size) |
| 99 | |
| 100 | def count_tx_frame(self, port, size): |
| 101 | for k, v in self.tx_counters.iteritems(): |
| 102 | if size >= v.min and size <= v.max: |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 103 | self.tx_counters[k].value[port - 1] += 1 |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 104 | return |
| 105 | log.warn("unsupported-packet-size", size=size) |
| 106 | |
| 107 | def log_counts(self): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 108 | rx_ct_list = [(v.name, v.value[0], v.value[1]) for v in |
| 109 | self.rx_counters.values()] |
| 110 | tx_ct_list = [(v.name, v.value[0], v.value[1]) for v in |
| 111 | self.tx_counters.values()] |
| 112 | log.info("rx-counts", rx_ct_list=rx_ct_list) |
| 113 | log.info("tx-counts", tx_ct_list=tx_ct_list) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 114 | |
| 115 | def make_proto(self): |
| 116 | sim_metrics = PonSimMetrics( |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 117 | device=self.device |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 118 | ) |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 119 | pon_port_metrics = PonSimPortMetrics( |
| 120 | port_name="pon" |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 121 | ) |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 122 | ni_port_metrics = PonSimPortMetrics( |
| 123 | port_name="nni" |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 124 | ) |
| 125 | for c in sorted(self.rx_counters): |
| 126 | ctr = self.rx_counters[c] |
| 127 | pon_port_metrics.packets.extend([ |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 128 | PonSimPacketCounter(name=ctr.name, value=ctr.value[0]) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 129 | ]) |
| 130 | # Since they're identical keys, save some time and cheat |
| 131 | ni_port_metrics.packets.extend([ |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 132 | PonSimPacketCounter(name=ctr.name, value=ctr.value[1]) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 133 | ]) |
| 134 | |
| 135 | for c in sorted(self.tx_counters): |
| 136 | ctr = self.tx_counters[c] |
| 137 | pon_port_metrics.packets.extend([ |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 138 | PonSimPacketCounter(name=ctr.name, value=ctr.value[0]) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 139 | ]) |
| 140 | # Since they're identical keys, save some time and cheat |
| 141 | ni_port_metrics.packets.extend([ |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 142 | PonSimPacketCounter(name=ctr.name, value=ctr.value[1]) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 143 | ]) |
| 144 | sim_metrics.metrics.extend([pon_port_metrics]) |
| 145 | sim_metrics.metrics.extend([ni_port_metrics]) |
| 146 | |
| 147 | return sim_metrics |
| 148 | |
| 149 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 150 | class SimDevice(object): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 151 | def __init__(self, name, logical_port_no): |
| 152 | self.name = name |
| 153 | self.logical_port_no = logical_port_no |
| 154 | self.links = dict() |
| 155 | self.flows = list() |
| 156 | self.log = structlog.get_logger(name=name, |
| 157 | logical_port_no=logical_port_no) |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 158 | self.counter = FrameIOCounter(name) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 159 | |
| 160 | def link(self, port, egress_fun): |
| 161 | self.links.setdefault(port, []).append(egress_fun) |
| 162 | |
| 163 | def ingress(self, port, frame): |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 164 | self.log.debug('ingress', ingress_port=port, name=self.name) |
| 165 | self.counter.count_rx_frame(port, len(frame["Ether"].payload)) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 166 | outcome = self.process_frame(port, frame) |
| 167 | if outcome is not None: |
| 168 | egress_port, egress_frame = outcome |
| 169 | forwarded = 0 |
| 170 | links = self.links.get(egress_port) |
| 171 | if links is not None: |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 172 | self.counter.count_tx_frame(egress_port, |
| 173 | len(egress_frame["Ether"].payload)) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 174 | for fun in links: |
| 175 | forwarded += 1 |
| 176 | self.log.debug('forwarding', egress_port=egress_port) |
| 177 | fun(egress_port, egress_frame) |
| 178 | if not forwarded: |
| 179 | self.log.debug('no-one-to-forward-to', egress_port=egress_port) |
| 180 | else: |
| 181 | self.log.debug('dropped') |
| 182 | |
| 183 | def install_flows(self, flows): |
| 184 | # store flows in precedence order so we can roll down on frame arrival |
| 185 | self.flows = sorted(flows, key=lambda fm: fm.priority, reverse=True) |
| 186 | |
| 187 | def process_frame(self, ingress_port, ingress_frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 188 | matched_mask = 0 |
| 189 | matched_flow = None |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 190 | for flow in self.flows: |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 191 | current_mask = self.is_match(flow, ingress_port, ingress_frame) |
| 192 | if current_mask > matched_mask: |
| 193 | matched_mask = current_mask |
| 194 | matched_flow = flow |
| 195 | |
| 196 | if matched_flow: |
| 197 | egress_port, egress_frame = self.process_actions( |
| 198 | matched_flow, ingress_frame) |
| 199 | return egress_port, egress_frame |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 200 | return None |
| 201 | |
| 202 | @staticmethod |
| 203 | def is_match(flow, ingress_port, frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 204 | matched_mask = 0 |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 205 | |
| 206 | def get_non_shim_ether_type(f): |
| 207 | if f.haslayer(Dot1Q): |
| 208 | f = f.getlayer(Dot1Q) |
| 209 | return f.type |
| 210 | |
| 211 | def get_vlan_pcp(f): |
| 212 | if f.haslayer(Dot1Q): |
| 213 | return f.getlayer(Dot1Q).prio |
| 214 | |
| 215 | def get_ip_proto(f): |
| 216 | if f.haslayer(IP): |
| 217 | return f.getlayer(IP).proto |
| 218 | |
| 219 | def get_ipv4_dst(f): |
| 220 | if f.haslayer(IP): |
| 221 | return f.getlayer(IP).dst |
| 222 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 223 | def get_udp_src(f): |
| 224 | if f.haslayer(UDP): |
| 225 | return f.getlayer(UDP).sport |
| 226 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 227 | def get_udp_dst(f): |
| 228 | if f.haslayer(UDP): |
| 229 | return f.getlayer(UDP).dport |
| 230 | |
| 231 | for field in get_ofb_fields(flow): |
| 232 | |
| 233 | if field.type == IN_PORT: |
| 234 | if field.port != ingress_port: |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 235 | return 0 |
| 236 | matched_mask |= _FlowMatchMask.IN_PORT |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 237 | |
| 238 | elif field.type == ETH_TYPE: |
| 239 | if field.eth_type != get_non_shim_ether_type(frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 240 | return 0 |
| 241 | matched_mask |= _FlowMatchMask.ETH_TYPE |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 242 | |
| 243 | elif field.type == IP_PROTO: |
| 244 | if field.ip_proto != get_ip_proto(frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 245 | return 0 |
| 246 | matched_mask |= _FlowMatchMask.IP_PROTO |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 247 | |
| 248 | elif field.type == VLAN_VID: |
| 249 | expected_vlan = field.vlan_vid |
| 250 | tagged = frame.haslayer(Dot1Q) |
| 251 | if bool(expected_vlan & 4096) != bool(tagged): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 252 | return 0 |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 253 | if tagged: |
| 254 | actual_vid = frame.getlayer(Dot1Q).vlan |
| 255 | if actual_vid != expected_vlan & 4095: |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 256 | return 0 |
| 257 | matched_mask |= _FlowMatchMask.VLAN_VID |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 258 | |
| 259 | elif field.type == VLAN_PCP: |
| 260 | if field.vlan_pcp != get_vlan_pcp(frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 261 | return 0 |
| 262 | matched_mask |= _FlowMatchMask.VLAN_PCP |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 263 | |
| 264 | elif field.type == IPV4_DST: |
| 265 | if ipv4int2str(field.ipv4_dst) != get_ipv4_dst(frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 266 | return 0 |
| 267 | matched_mask |= _FlowMatchMask.IPV4_DST |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 268 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 269 | elif field.type == UDP_SRC: |
| 270 | if field.udp_src != get_udp_src(frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 271 | return 0 |
| 272 | matched_mask |= _FlowMatchMask.UDP_SRC |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 273 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 274 | elif field.type == UDP_DST: |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 275 | if field.udp_dst != get_udp_dst(frame): |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 276 | return 0 |
| 277 | matched_mask |= _FlowMatchMask.UDP_DST |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 278 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 279 | elif field.type == METADATA: |
| 280 | pass # safe to ignore |
| 281 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 282 | else: |
| 283 | raise NotImplementedError('field.type=%d' % field.type) |
| 284 | |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 285 | return matched_mask |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 286 | |
| 287 | @staticmethod |
| 288 | def process_actions(flow, frame): |
| 289 | egress_port = None |
| 290 | for action in get_actions(flow): |
| 291 | |
| 292 | if action.type == OUTPUT: |
| 293 | egress_port = action.output.port |
| 294 | |
| 295 | elif action.type == POP_VLAN: |
| 296 | if frame.haslayer(Dot1Q): |
| 297 | shim = frame.getlayer(Dot1Q) |
| 298 | frame = Ether( |
| 299 | src=frame.src, |
| 300 | dst=frame.dst, |
| 301 | type=shim.type) / shim.payload |
| 302 | |
| 303 | elif action.type == PUSH_VLAN: |
| 304 | frame = ( |
| 305 | Ether(src=frame.src, dst=frame.dst, |
| 306 | type=action.push.ethertype) / |
| 307 | Dot1Q(type=frame.type) / |
| 308 | frame.payload |
| 309 | ) |
| 310 | |
| 311 | elif action.type == SET_FIELD: |
| 312 | assert (action.set_field.field.oxm_class == |
| 313 | ofp.OFPXMC_OPENFLOW_BASIC) |
| 314 | field = action.set_field.field.ofb_field |
| 315 | |
| 316 | if field.type == VLAN_VID: |
| 317 | shim = frame.getlayer(Dot1Q) |
| 318 | shim.vlan = field.vlan_vid & 4095 |
| 319 | |
| 320 | elif field.type == VLAN_PCP: |
| 321 | shim = frame.getlayer(Dot1Q) |
| 322 | shim.prio = field.vlan_pcp |
| 323 | |
| 324 | else: |
| 325 | raise NotImplementedError('set_field.field.type=%d' |
| 326 | % field.type) |
| 327 | |
| 328 | else: |
| 329 | raise NotImplementedError('action.type=%d' % action.type) |
| 330 | |
| 331 | return egress_port, frame |
| 332 | |
| 333 | |
| 334 | class PonSim(object): |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 335 | def __init__(self, onus, egress_fun): |
| 336 | self.egress_fun = egress_fun |
| 337 | |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 338 | self.log = structlog.get_logger() |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 339 | # Create OLT and hook NNI port up for egress |
| 340 | self.olt = SimDevice('olt', 0) |
| 341 | self.olt.link(2, lambda _, frame: self.egress_fun(0, frame)) |
| 342 | self.devices = dict() |
| 343 | self.devices[0] = self.olt |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 344 | # TODO: This can be removed, it's for debugging purposes |
| 345 | self.lc = LoopingCall(self.olt.counter.log_counts) |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 346 | self.lc.start(90) # To correlate with Kafka |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 347 | |
| 348 | # Create ONUs of the requested number and hook them up with OLT |
| 349 | # and with egress fun |
| 350 | def mk_egress_fun(port_no): |
| 351 | return lambda _, frame: self.egress_fun(port_no, frame) |
| 352 | |
| 353 | def mk_onu_ingress(onu): |
| 354 | return lambda _, frame: onu.ingress(1, frame) |
| 355 | |
| 356 | for i in range(onus): |
| 357 | port_no = 128 + i |
| 358 | onu = SimDevice('onu%d' % i, port_no) |
Khen Nursimulu | 4979214 | 2017-03-17 12:34:05 -0400 | [diff] [blame] | 359 | onu.link(1, lambda _, frame: self.olt.ingress(1, |
| 360 | frame)) # Send to the OLT |
| 361 | onu.link(2, |
| 362 | mk_egress_fun(port_no)) # Send from the ONU to the world |
| 363 | self.olt.link(1, mk_onu_ingress(onu)) # Internal send to the ONU |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 364 | self.devices[port_no] = onu |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 365 | for d in self.devices: |
| 366 | self.log.info("pon-sim-init", port=d, name=self.devices[d].name, |
| 367 | links=self.devices[d].links) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 368 | |
| 369 | def get_ports(self): |
| 370 | return sorted(self.devices.keys()) |
| 371 | |
Sergio Slobodrian | 98eff41 | 2017-03-15 14:46:30 -0400 | [diff] [blame] | 372 | def get_stats(self): |
| 373 | return self.olt.counter.make_proto() |
| 374 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 375 | def olt_install_flows(self, flows): |
| 376 | self.olt.install_flows(flows) |
| 377 | |
| 378 | def onu_install_flows(self, onu_port, flows): |
| 379 | self.devices[onu_port].install_flows(flows) |
| 380 | |
| 381 | def ingress(self, port, frame): |
| 382 | if not isinstance(frame, Packet): |
| 383 | frame = Ether(frame) |
| 384 | self.devices[port].ingress(2, frame) |