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 | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 21 | "encoding/json" |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 22 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 23 | "github.com/donNewtonAlpha/goloxi" |
| 24 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes/empty" |
| 26 | "github.com/opencord/ofagent-go/openflow" |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 27 | "github.com/opencord/ofagent-go/settings" |
| 28 | l "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v2/go/openflow_13" |
| 30 | pb "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 31 | "google.golang.org/grpc" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | func receivePacketIn(client pb.VolthaServiceClient) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 35 | if settings.GetDebug(grpcDeviceID) { |
| 36 | logger.Debugln("Starting ReceivePacketIn Stream") |
| 37 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 38 | opt := grpc.EmptyCallOption{} |
| 39 | stream, err := client.ReceivePacketsIn(context.Background(), &empty.Empty{}, opt) |
| 40 | if err != nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 41 | logger.Fatalw("Unable to establish PacketIn stream", l.Fields{"Error": err}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 42 | } |
| 43 | for { |
| 44 | packet, err := stream.Recv() |
| 45 | packetIn := packet.GetPacketIn() |
| 46 | |
| 47 | if err != nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 48 | logger.Fatalw("ReceivePacketIn unable to receive packet", l.Fields{"Error": err}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 49 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 50 | if settings.GetDebug(grpcDeviceID) { |
| 51 | js, _ := json.Marshal(packetIn) |
| 52 | logger.Debugw("ReceivePacketIn Recieved", l.Fields{"PacketIn": js}) |
| 53 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 54 | deviceID := packet.GetId() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 55 | 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 61 | match := ofp.NewMatchV3() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 62 | inMatch := packetIn.GetMatch() |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 63 | 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 Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 120 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 121 | fields = append(fields, ofpVlanVid) |
| 122 | default: |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame^] | 123 | logger.Warnw("receivePacketIn Unhandled OxmField ", l.Fields{"Field": ofbField.Type}) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | match.SetLength(size) |
| 127 | |
| 128 | match.SetOxmList(fields) |
| 129 | |
| 130 | ofPacketIn.SetMatch(*match) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 131 | ofPacketIn.SetReason(uint8(packetIn.GetReason())) |
| 132 | ofPacketIn.SetTableId(uint8(packetIn.GetTableId())) |
| 133 | ofPacketIn.SetTotalLen(uint16(len(ofPacketIn.GetData()))) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 134 | openFlowClient := GetClient(deviceID) |
| 135 | openFlowClient.SendMessage(ofPacketIn) |
| 136 | |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } |