Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2019 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 | import threading |
| 18 | from google.protobuf.json_format import Parse |
| 19 | from simplejson import loads |
| 20 | import structlog |
| 21 | from scapy.layers.l2 import Ether, Dot1Q |
| 22 | import binascii |
| 23 | |
| 24 | from common.frameio.frameio import hexify |
| 25 | from voltha.protos.openflow_13_pb2 import PacketOut |
| 26 | from voltha.adapters.openolt.openolt_kafka_consumer import KConsumer |
| 27 | from voltha.core.flow_decomposer import OUTPUT |
| 28 | from voltha.protos.device_pb2 import Port |
| 29 | from voltha.adapters.openolt.protos import openolt_pb2 |
| 30 | |
| 31 | |
| 32 | class OpenoltPacket(object): |
| 33 | def __init__(self, device): |
| 34 | self.log = structlog.get_logger() |
| 35 | self.device = device |
Shad Ansari | 8793c13 | 2019-05-18 00:02:27 +0000 | [diff] [blame] | 36 | self.packet_thread_handle = threading.Thread( |
| 37 | target=self.packet_thread) |
| 38 | self.packet_thread_handle.setDaemon(True) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 39 | |
| 40 | def start(self): |
Shad Ansari | 8793c13 | 2019-05-18 00:02:27 +0000 | [diff] [blame] | 41 | self.packet_thread_handle.start() |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 42 | |
| 43 | def stop(self): |
Shad Ansari | 1ed6f7d | 2019-05-18 00:01:54 +0000 | [diff] [blame] | 44 | pass |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 45 | |
Shad Ansari | 8793c13 | 2019-05-18 00:02:27 +0000 | [diff] [blame] | 46 | def packet_thread(self): |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 47 | self.log.debug('openolt packet-out thread starting') |
Shad Ansari | 8793c13 | 2019-05-18 00:02:27 +0000 | [diff] [blame] | 48 | KConsumer(self.packet_process, |
Shad Ansari | a3bcfe1 | 2019-04-13 11:46:28 -0700 | [diff] [blame] | 49 | 'voltha.pktout-{}'.format( |
| 50 | self.device.data_model.logical_device_id)) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 51 | |
Shad Ansari | 8793c13 | 2019-05-18 00:02:27 +0000 | [diff] [blame] | 52 | def packet_process(self, topic, msg): |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 53 | |
| 54 | def get_port_out(opo): |
| 55 | for action in opo.actions: |
| 56 | if action.type == OUTPUT: |
| 57 | return action.output.port |
| 58 | |
| 59 | pb = Parse(loads(msg), PacketOut(), ignore_unknown_fields=True) |
| 60 | |
| 61 | logical_device_id = pb.id |
| 62 | ofp_packet_out = pb.packet_out |
| 63 | |
| 64 | self.log.debug("received packet-out form kafka", |
| 65 | logical_device_id=logical_device_id, |
| 66 | ofp_packet_out=ofp_packet_out) |
| 67 | |
| 68 | egress_port = get_port_out(ofp_packet_out) |
| 69 | msg = ofp_packet_out.data |
| 70 | |
| 71 | self.log.debug('rcv-packet-out', logical_device_id=logical_device_id, |
| 72 | egress_port=egress_port, |
| 73 | # adapter_name=self.adapter_name, |
| 74 | data=hexify(msg)) |
| 75 | |
| 76 | pkt = Ether(msg) |
| 77 | self.log.debug('packet out', egress_port=egress_port, |
| 78 | packet=str(pkt).encode("HEX")) |
| 79 | |
| 80 | # Find port type |
| 81 | egress_port_type = self.device.platform \ |
| 82 | .intf_id_to_port_type_name(egress_port) |
| 83 | |
| 84 | if egress_port_type == Port.ETHERNET_UNI: |
| 85 | |
| 86 | if pkt.haslayer(Dot1Q): |
| 87 | outer_shim = pkt.getlayer(Dot1Q) |
| 88 | if isinstance(outer_shim.payload, Dot1Q): |
| 89 | # If double tag, remove the outer tag |
| 90 | payload = ( |
| 91 | Ether(src=pkt.src, dst=pkt.dst, |
| 92 | type=outer_shim.type) / |
| 93 | outer_shim.payload |
| 94 | ) |
| 95 | else: |
| 96 | payload = pkt |
| 97 | else: |
| 98 | payload = pkt |
| 99 | |
| 100 | send_pkt = binascii.unhexlify(str(payload).encode("HEX")) |
| 101 | |
| 102 | self.log.debug( |
| 103 | 'sending-packet-to-ONU', egress_port=egress_port, |
| 104 | intf_id=self.device.platform.intf_id_from_uni_port_num( |
| 105 | egress_port), |
| 106 | onu_id=self.device.platform.onu_id_from_port_num(egress_port), |
| 107 | uni_id=self.device.platform.uni_id_from_port_num(egress_port), |
| 108 | port_no=egress_port, |
| 109 | packet=str(payload).encode("HEX")) |
| 110 | |
| 111 | onu_pkt = openolt_pb2.OnuPacket( |
| 112 | intf_id=self.device.platform.intf_id_from_uni_port_num( |
| 113 | egress_port), |
| 114 | onu_id=self.device.platform.onu_id_from_port_num(egress_port), |
| 115 | port_no=egress_port, |
| 116 | pkt=send_pkt) |
| 117 | |
Shad Ansari | 548f94d | 2019-04-24 13:42:52 -0700 | [diff] [blame] | 118 | self.device.stub.OnuPacketOut(onu_pkt) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 119 | |
| 120 | elif egress_port_type == Port.ETHERNET_NNI: |
| 121 | self.log.debug('sending-packet-to-uplink', egress_port=egress_port, |
| 122 | packet=str(pkt).encode("HEX")) |
| 123 | |
| 124 | send_pkt = binascii.unhexlify(str(pkt).encode("HEX")) |
| 125 | |
| 126 | uplink_pkt = openolt_pb2.UplinkPacket( |
| 127 | intf_id=self.device.platform.intf_id_from_nni_port_num( |
| 128 | egress_port), |
| 129 | pkt=send_pkt) |
| 130 | |
Shad Ansari | 548f94d | 2019-04-24 13:42:52 -0700 | [diff] [blame] | 131 | self.device.stub.UplinkPacketOut(uplink_pkt) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 132 | |
| 133 | else: |
| 134 | self.log.warn('Packet-out-to-this-interface-type-not-implemented', |
| 135 | egress_port=egress_port, |
| 136 | port_type=egress_port_type) |