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