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 |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 23 | from scapy.layers.l2 import Packet |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 24 | |
| 25 | from common.frameio.frameio import hexify |
| 26 | from voltha.protos.openflow_13_pb2 import PacketOut |
| 27 | from voltha.adapters.openolt.openolt_kafka_consumer import KConsumer |
| 28 | from voltha.core.flow_decomposer import OUTPUT |
| 29 | from voltha.protos.device_pb2 import Port |
| 30 | from voltha.adapters.openolt.protos import openolt_pb2 |
| 31 | |
| 32 | |
| 33 | class OpenoltPacket(object): |
| 34 | def __init__(self, device): |
| 35 | self.log = structlog.get_logger() |
| 36 | self.device = device |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 37 | |
| 38 | self.packet_out_thread_handle = threading.Thread( |
| 39 | target=self.packet_out_thread) |
| 40 | self.packet_out_thread_handle.setDaemon(True) |
| 41 | |
| 42 | self.packet_in_thread_handle = threading.Thread( |
| 43 | target=self.packet_in_thread) |
| 44 | self.packet_in_thread_handle.setDaemon(True) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 45 | |
| 46 | def start(self): |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 47 | self.packet_out_thread_handle.start() |
| 48 | self.packet_in_thread_handle.start() |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 49 | |
| 50 | def stop(self): |
Shad Ansari | 1ed6f7d | 2019-05-18 00:01:54 +0000 | [diff] [blame] | 51 | pass |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 52 | |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 53 | def packet_out_thread(self): |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 54 | self.log.debug('openolt packet-out thread starting') |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 55 | KConsumer(self.packet_out_process, |
Shad Ansari | a3bcfe1 | 2019-04-13 11:46:28 -0700 | [diff] [blame] | 56 | 'voltha.pktout-{}'.format( |
| 57 | self.device.data_model.logical_device_id)) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 58 | |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 59 | def packet_in_thread(self): |
| 60 | self.log.debug('openolt packet-in thread starting') |
| 61 | topic = 'openolt.pktin-{}'.format( |
| 62 | self.device.host_and_port.split(':')[0]) |
| 63 | KConsumer(self.packet_in_process, topic) |
| 64 | |
| 65 | def packet_out_process(self, topic, msg): |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 66 | |
| 67 | def get_port_out(opo): |
| 68 | for action in opo.actions: |
| 69 | if action.type == OUTPUT: |
| 70 | return action.output.port |
| 71 | |
| 72 | pb = Parse(loads(msg), PacketOut(), ignore_unknown_fields=True) |
| 73 | |
| 74 | logical_device_id = pb.id |
| 75 | ofp_packet_out = pb.packet_out |
| 76 | |
| 77 | self.log.debug("received packet-out form kafka", |
| 78 | logical_device_id=logical_device_id, |
| 79 | ofp_packet_out=ofp_packet_out) |
| 80 | |
| 81 | egress_port = get_port_out(ofp_packet_out) |
| 82 | msg = ofp_packet_out.data |
| 83 | |
| 84 | self.log.debug('rcv-packet-out', logical_device_id=logical_device_id, |
| 85 | egress_port=egress_port, |
| 86 | # adapter_name=self.adapter_name, |
| 87 | data=hexify(msg)) |
| 88 | |
| 89 | pkt = Ether(msg) |
| 90 | self.log.debug('packet out', egress_port=egress_port, |
| 91 | packet=str(pkt).encode("HEX")) |
| 92 | |
| 93 | # Find port type |
| 94 | egress_port_type = self.device.platform \ |
| 95 | .intf_id_to_port_type_name(egress_port) |
| 96 | |
| 97 | if egress_port_type == Port.ETHERNET_UNI: |
| 98 | |
| 99 | if pkt.haslayer(Dot1Q): |
| 100 | outer_shim = pkt.getlayer(Dot1Q) |
| 101 | if isinstance(outer_shim.payload, Dot1Q): |
| 102 | # If double tag, remove the outer tag |
| 103 | payload = ( |
| 104 | Ether(src=pkt.src, dst=pkt.dst, |
| 105 | type=outer_shim.type) / |
| 106 | outer_shim.payload |
| 107 | ) |
| 108 | else: |
| 109 | payload = pkt |
| 110 | else: |
| 111 | payload = pkt |
| 112 | |
| 113 | send_pkt = binascii.unhexlify(str(payload).encode("HEX")) |
| 114 | |
| 115 | self.log.debug( |
| 116 | 'sending-packet-to-ONU', egress_port=egress_port, |
| 117 | intf_id=self.device.platform.intf_id_from_uni_port_num( |
| 118 | egress_port), |
| 119 | onu_id=self.device.platform.onu_id_from_port_num(egress_port), |
| 120 | uni_id=self.device.platform.uni_id_from_port_num(egress_port), |
| 121 | port_no=egress_port, |
| 122 | packet=str(payload).encode("HEX")) |
| 123 | |
| 124 | onu_pkt = openolt_pb2.OnuPacket( |
| 125 | intf_id=self.device.platform.intf_id_from_uni_port_num( |
| 126 | egress_port), |
| 127 | onu_id=self.device.platform.onu_id_from_port_num(egress_port), |
| 128 | port_no=egress_port, |
| 129 | pkt=send_pkt) |
| 130 | |
Shad Ansari | 548f94d | 2019-04-24 13:42:52 -0700 | [diff] [blame] | 131 | self.device.stub.OnuPacketOut(onu_pkt) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 132 | |
| 133 | elif egress_port_type == Port.ETHERNET_NNI: |
| 134 | self.log.debug('sending-packet-to-uplink', egress_port=egress_port, |
| 135 | packet=str(pkt).encode("HEX")) |
| 136 | |
| 137 | send_pkt = binascii.unhexlify(str(pkt).encode("HEX")) |
| 138 | |
| 139 | uplink_pkt = openolt_pb2.UplinkPacket( |
| 140 | intf_id=self.device.platform.intf_id_from_nni_port_num( |
| 141 | egress_port), |
| 142 | pkt=send_pkt) |
| 143 | |
Shad Ansari | 548f94d | 2019-04-24 13:42:52 -0700 | [diff] [blame] | 144 | self.device.stub.UplinkPacketOut(uplink_pkt) |
Shad Ansari | 42392a7 | 2019-04-09 22:44:18 -0700 | [diff] [blame] | 145 | |
| 146 | else: |
| 147 | self.log.warn('Packet-out-to-this-interface-type-not-implemented', |
| 148 | egress_port=egress_port, |
| 149 | port_type=egress_port_type) |
Shad Ansari | 12acc9a | 2019-05-20 21:46:28 +0000 | [diff] [blame^] | 150 | |
| 151 | def packet_in_process(self, topic, msg): |
| 152 | |
| 153 | ind = Parse(loads(msg), openolt_pb2.Indication(), |
| 154 | ignore_unknown_fields=True) |
| 155 | assert(ind.HasField('pkt_ind')) |
| 156 | pkt_ind = ind.pkt_ind |
| 157 | |
| 158 | self.log.debug("packet indication", |
| 159 | intf_type=pkt_ind.intf_type, |
| 160 | intf_id=pkt_ind.intf_id, |
| 161 | port_no=pkt_ind.port_no, |
| 162 | cookie=pkt_ind.cookie, |
| 163 | gemport_id=pkt_ind.gemport_id, |
| 164 | flow_id=pkt_ind.flow_id) |
| 165 | try: |
| 166 | logical_port_num = self.device.data_model.logical_port_num( |
| 167 | pkt_ind.intf_type, |
| 168 | pkt_ind.intf_id, |
| 169 | pkt_ind.port_no, |
| 170 | pkt_ind.gemport_id) |
| 171 | except ValueError: |
| 172 | self.log.error('No logical port found', |
| 173 | intf_type=pkt_ind.intf_type, |
| 174 | intf_id=pkt_ind.intf_id, |
| 175 | port_no=pkt_ind.port_no, |
| 176 | gemport_id=pkt_ind.gemport_id) |
| 177 | return |
| 178 | |
| 179 | ether_pkt = Ether(pkt_ind.pkt) |
| 180 | |
| 181 | if isinstance(ether_pkt, Packet): |
| 182 | ether_pkt = str(ether_pkt) |
| 183 | |
| 184 | logical_device_id = self.device.data_model.logical_device_id |
| 185 | topic = 'packet-in:' + logical_device_id |
| 186 | |
| 187 | self.log.debug('send-packet-in', logical_device_id=logical_device_id, |
| 188 | logical_port_num=logical_port_num, |
| 189 | packet=hexify(ether_pkt)) |
| 190 | |
| 191 | self.device.data_model.adapter_agent.event_bus.publish( |
| 192 | topic, (logical_port_num, str(ether_pkt))) |