blob: bc8d3a85b3b230d78f8697abf2bca289a966a775 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
2 Copyright 2017 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
17package grpc
18
19import (
20 "context"
Don Newtonb437c6f2019-12-18 11:51:57 -050021 "encoding/json"
Don Newton7577f072020-01-06 12:41:11 -050022
Don Newtone0d34a82019-11-14 10:58:06 -050023 "github.com/donNewtonAlpha/goloxi"
24 ofp "github.com/donNewtonAlpha/goloxi/of13"
Don Newton98fd8812019-09-23 15:15:02 -040025 "github.com/golang/protobuf/ptypes/empty"
26 "github.com/opencord/ofagent-go/openflow"
Don Newton7577f072020-01-06 12:41:11 -050027 "github.com/opencord/ofagent-go/settings"
28 l "github.com/opencord/voltha-lib-go/v2/pkg/log"
Don Newtonb437c6f2019-12-18 11:51:57 -050029 "github.com/opencord/voltha-protos/v2/go/openflow_13"
30 pb "github.com/opencord/voltha-protos/v2/go/voltha"
Don Newton98fd8812019-09-23 15:15:02 -040031 "google.golang.org/grpc"
Don Newton98fd8812019-09-23 15:15:02 -040032)
33
34func receivePacketIn(client pb.VolthaServiceClient) {
Don Newton7577f072020-01-06 12:41:11 -050035 if settings.GetDebug(grpcDeviceID) {
36 logger.Debugln("Starting ReceivePacketIn Stream")
37 }
Don Newton98fd8812019-09-23 15:15:02 -040038 opt := grpc.EmptyCallOption{}
39 stream, err := client.ReceivePacketsIn(context.Background(), &empty.Empty{}, opt)
40 if err != nil {
Don Newton7577f072020-01-06 12:41:11 -050041 logger.Fatalw("Unable to establish PacketIn stream", l.Fields{"Error": err})
Don Newton98fd8812019-09-23 15:15:02 -040042 }
43 for {
44 packet, err := stream.Recv()
45 packetIn := packet.GetPacketIn()
46
47 if err != nil {
Don Newton7577f072020-01-06 12:41:11 -050048 logger.Fatalw("ReceivePacketIn unable to receive packet", l.Fields{"Error": err})
Don Newton98fd8812019-09-23 15:15:02 -040049 }
Don Newton7577f072020-01-06 12:41:11 -050050 if settings.GetDebug(grpcDeviceID) {
51 js, _ := json.Marshal(packetIn)
52 logger.Debugw("ReceivePacketIn Recieved", l.Fields{"PacketIn": js})
53 }
Don Newtone0d34a82019-11-14 10:58:06 -050054 deviceID := packet.GetId()
Don Newton98fd8812019-09-23 15:15:02 -040055 ofPacketIn := ofp.NewPacketIn()
56 ofPacketIn.SetVersion(uint8(4))
57 ofPacketIn.SetXid(openflow.GetXid())
58 ofPacketIn.SetBufferId(packetIn.GetBufferId())
59 ofPacketIn.SetCookie(packetIn.GetCookie())
60 ofPacketIn.SetData(packetIn.GetData())
Don Newtone0d34a82019-11-14 10:58:06 -050061 match := ofp.NewMatchV3()
Don Newton98fd8812019-09-23 15:15:02 -040062 inMatch := packetIn.GetMatch()
Don Newtone0d34a82019-11-14 10:58:06 -050063 match.SetType(uint16(inMatch.GetType()))
64 oxFields := inMatch.GetOxmFields()
65 var fields []goloxi.IOxm
66 var size uint16
67 size = 4
68 for i := 0; i < len(oxFields); i++ {
69 oxmField := oxFields[i]
70 field := oxmField.GetField()
71 ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField
72 size += 4 //header for oxm
73 switch ofbField.Type {
74 case pb.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT:
75 ofpInPort := ofp.NewOxmInPort()
76 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port)
77 ofpInPort.Value = ofp.Port(val.Port)
78 size += 4
79 fields = append(fields, ofpInPort)
80 case pb.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE:
81 ofpEthType := ofp.NewOxmEthType()
82 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType)
83 ofpEthType.Value = ofp.EthernetType(val.EthType)
84 size += 2
85 fields = append(fields, ofpEthType)
86 case pb.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT:
87 ofpInPhyPort := ofp.NewOxmInPhyPort()
88 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort)
89 ofpInPhyPort.Value = ofp.Port(val.PhysicalPort)
90 size += 4
91 fields = append(fields, ofpInPhyPort)
92 case pb.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO:
93 ofpIpProto := ofp.NewOxmIpProto()
94 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto)
95 ofpIpProto.Value = ofp.IpPrototype(val.IpProto)
96 size += 1
97 fields = append(fields, ofpIpProto)
98 case pb.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC:
99 ofpUdpSrc := ofp.NewOxmUdpSrc()
100 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc)
101 ofpUdpSrc.Value = uint16(val.UdpSrc)
102 size += 2
103 fields = append(fields, ofpUdpSrc)
104 case pb.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST:
105 ofpUdpDst := ofp.NewOxmUdpDst()
106 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst)
107 ofpUdpDst.Value = uint16(val.UdpDst)
108 size += 2
109 fields = append(fields, ofpUdpDst)
110 case pb.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID:
111 ofpVlanVid := ofp.NewOxmVlanVid()
112 val := ofbField.GetValue()
113 if val != nil {
114 vlanId := val.(*openflow_13.OfpOxmOfbField_VlanVid)
115 ofpVlanVid.Value = uint16(vlanId.VlanVid) + 0x1000
116 size += 2
117 } else {
118 ofpVlanVid.Value = uint16(0)
119 }
Don Newton98fd8812019-09-23 15:15:02 -0400120
Don Newtone0d34a82019-11-14 10:58:06 -0500121 fields = append(fields, ofpVlanVid)
122 default:
Don Newton7577f072020-01-06 12:41:11 -0500123 logger.Warnw("receivePacketIn Unhandled OxmField ", l.Fields{"Field": ofbField.Type})
Don Newtone0d34a82019-11-14 10:58:06 -0500124 }
125 }
126 match.SetLength(size)
127
128 match.SetOxmList(fields)
129
130 ofPacketIn.SetMatch(*match)
Don Newton98fd8812019-09-23 15:15:02 -0400131 ofPacketIn.SetReason(uint8(packetIn.GetReason()))
132 ofPacketIn.SetTableId(uint8(packetIn.GetTableId()))
133 ofPacketIn.SetTotalLen(uint16(len(ofPacketIn.GetData())))
Don Newtone0d34a82019-11-14 10:58:06 -0500134 openFlowClient := GetClient(deviceID)
135 openFlowClient.SendMessage(ofPacketIn)
136
Don Newton98fd8812019-09-23 15:15:02 -0400137 }
138
139}