blob: 44e537b2ec204024cf516d739c3f72e0b71c0046 [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 Newtone0d34a82019-11-14 10:58:06 -050022 "github.com/donNewtonAlpha/goloxi"
23 ofp "github.com/donNewtonAlpha/goloxi/of13"
Don Newton98fd8812019-09-23 15:15:02 -040024 "github.com/golang/protobuf/ptypes/empty"
25 "github.com/opencord/ofagent-go/openflow"
Don Newtonb437c6f2019-12-18 11:51:57 -050026 "github.com/opencord/voltha-protos/v2/go/openflow_13"
27 pb "github.com/opencord/voltha-protos/v2/go/voltha"
Don Newton98fd8812019-09-23 15:15:02 -040028 "google.golang.org/grpc"
29 "log"
30)
31
32func receivePacketIn(client pb.VolthaServiceClient) {
33 opt := grpc.EmptyCallOption{}
34 stream, err := client.ReceivePacketsIn(context.Background(), &empty.Empty{}, opt)
35 if err != nil {
36 log.Fatalln("Unable to establish stream")
37 }
38 for {
39 packet, err := stream.Recv()
40 packetIn := packet.GetPacketIn()
41
42 if err != nil {
43 log.Fatalf("error on stream.Rec %v", err)
44 }
Don Newtonb437c6f2019-12-18 11:51:57 -050045 js, _ := json.Marshal(packetIn)
46 log.Printf("PACKET IN %s", js)
Don Newtone0d34a82019-11-14 10:58:06 -050047 deviceID := packet.GetId()
Don Newton98fd8812019-09-23 15:15:02 -040048 ofPacketIn := ofp.NewPacketIn()
49 ofPacketIn.SetVersion(uint8(4))
50 ofPacketIn.SetXid(openflow.GetXid())
51 ofPacketIn.SetBufferId(packetIn.GetBufferId())
52 ofPacketIn.SetCookie(packetIn.GetCookie())
53 ofPacketIn.SetData(packetIn.GetData())
Don Newtone0d34a82019-11-14 10:58:06 -050054 match := ofp.NewMatchV3()
Don Newton98fd8812019-09-23 15:15:02 -040055 inMatch := packetIn.GetMatch()
Don Newtone0d34a82019-11-14 10:58:06 -050056 match.SetType(uint16(inMatch.GetType()))
57 oxFields := inMatch.GetOxmFields()
58 var fields []goloxi.IOxm
59 var size uint16
60 size = 4
61 for i := 0; i < len(oxFields); i++ {
62 oxmField := oxFields[i]
63 field := oxmField.GetField()
64 ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField
65 size += 4 //header for oxm
66 switch ofbField.Type {
67 case pb.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT:
68 ofpInPort := ofp.NewOxmInPort()
69 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port)
70 ofpInPort.Value = ofp.Port(val.Port)
71 size += 4
72 fields = append(fields, ofpInPort)
73 case pb.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE:
74 ofpEthType := ofp.NewOxmEthType()
75 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType)
76 ofpEthType.Value = ofp.EthernetType(val.EthType)
77 size += 2
78 fields = append(fields, ofpEthType)
79 case pb.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT:
80 ofpInPhyPort := ofp.NewOxmInPhyPort()
81 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort)
82 ofpInPhyPort.Value = ofp.Port(val.PhysicalPort)
83 size += 4
84 fields = append(fields, ofpInPhyPort)
85 case pb.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO:
86 ofpIpProto := ofp.NewOxmIpProto()
87 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto)
88 ofpIpProto.Value = ofp.IpPrototype(val.IpProto)
89 size += 1
90 fields = append(fields, ofpIpProto)
91 case pb.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC:
92 ofpUdpSrc := ofp.NewOxmUdpSrc()
93 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc)
94 ofpUdpSrc.Value = uint16(val.UdpSrc)
95 size += 2
96 fields = append(fields, ofpUdpSrc)
97 case pb.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST:
98 ofpUdpDst := ofp.NewOxmUdpDst()
99 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst)
100 ofpUdpDst.Value = uint16(val.UdpDst)
101 size += 2
102 fields = append(fields, ofpUdpDst)
103 case pb.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID:
104 ofpVlanVid := ofp.NewOxmVlanVid()
105 val := ofbField.GetValue()
106 if val != nil {
107 vlanId := val.(*openflow_13.OfpOxmOfbField_VlanVid)
108 ofpVlanVid.Value = uint16(vlanId.VlanVid) + 0x1000
109 size += 2
110 } else {
111 ofpVlanVid.Value = uint16(0)
112 }
Don Newton98fd8812019-09-23 15:15:02 -0400113
Don Newtone0d34a82019-11-14 10:58:06 -0500114 fields = append(fields, ofpVlanVid)
115 default:
116 log.Printf("handleFlowStatsRequest Unhandled OxmField %v", ofbField.Type)
117 }
118 }
119 match.SetLength(size)
120
121 match.SetOxmList(fields)
122
123 ofPacketIn.SetMatch(*match)
Don Newton98fd8812019-09-23 15:15:02 -0400124 ofPacketIn.SetReason(uint8(packetIn.GetReason()))
125 ofPacketIn.SetTableId(uint8(packetIn.GetTableId()))
126 ofPacketIn.SetTotalLen(uint16(len(ofPacketIn.GetData())))
Don Newtone0d34a82019-11-14 10:58:06 -0500127 openFlowClient := GetClient(deviceID)
128 openFlowClient.SendMessage(ofPacketIn)
129
Don Newton98fd8812019-09-23 15:15:02 -0400130 }
131
132}