blob: 318456e570deb5a642695bc30971a206b90a9ca6 [file] [log] [blame]
Shad Ansari42392a72019-04-09 22:44:18 -07001#
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
17import threading
18from google.protobuf.json_format import Parse
19from simplejson import loads
20import structlog
21from scapy.layers.l2 import Ether, Dot1Q
22import binascii
Shad Ansari12acc9a2019-05-20 21:46:28 +000023from scapy.layers.l2 import Packet
Shad Ansari42392a72019-04-09 22:44:18 -070024
25from common.frameio.frameio import hexify
26from voltha.protos.openflow_13_pb2 import PacketOut
27from voltha.adapters.openolt.openolt_kafka_consumer import KConsumer
28from voltha.core.flow_decomposer import OUTPUT
29from voltha.protos.device_pb2 import Port
30from voltha.adapters.openolt.protos import openolt_pb2
31
32
33class OpenoltPacket(object):
34 def __init__(self, device):
35 self.log = structlog.get_logger()
36 self.device = device
Shad Ansari12acc9a2019-05-20 21:46:28 +000037
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 Ansari42392a72019-04-09 22:44:18 -070045
46 def start(self):
Shad Ansari12acc9a2019-05-20 21:46:28 +000047 self.packet_out_thread_handle.start()
48 self.packet_in_thread_handle.start()
Shad Ansari42392a72019-04-09 22:44:18 -070049
50 def stop(self):
Shad Ansari1ed6f7d2019-05-18 00:01:54 +000051 pass
Shad Ansari42392a72019-04-09 22:44:18 -070052
Shad Ansari12acc9a2019-05-20 21:46:28 +000053 def packet_out_thread(self):
Shad Ansari42392a72019-04-09 22:44:18 -070054 self.log.debug('openolt packet-out thread starting')
Shad Ansari12acc9a2019-05-20 21:46:28 +000055 KConsumer(self.packet_out_process,
Shad Ansaria3bcfe12019-04-13 11:46:28 -070056 'voltha.pktout-{}'.format(
57 self.device.data_model.logical_device_id))
Shad Ansari42392a72019-04-09 22:44:18 -070058
Shad Ansari12acc9a2019-05-20 21:46:28 +000059 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 Ansari42392a72019-04-09 22:44:18 -070066
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 Ansari548f94d2019-04-24 13:42:52 -0700131 self.device.stub.OnuPacketOut(onu_pkt)
Shad Ansari42392a72019-04-09 22:44:18 -0700132
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 Ansari548f94d2019-04-24 13:42:52 -0700144 self.device.stub.UplinkPacketOut(uplink_pkt)
Shad Ansari42392a72019-04-09 22:44:18 -0700145
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 Ansari12acc9a2019-05-20 21:46:28 +0000150
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)))