David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 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 ofagent |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
David Bainbridge | 20e34e7 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 22 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 23 | "github.com/donNewtonAlpha/goloxi" |
| 24 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
| 25 | "github.com/golang/protobuf/ptypes/empty" |
| 26 | "github.com/opencord/ofagent-go/internal/pkg/openflow" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 28 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 29 | "github.com/opencord/voltha-protos/v3/go/voltha" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 30 | "google.golang.org/grpc" |
| 31 | ) |
| 32 | |
| 33 | func (ofa *OFAgent) receivePacketsIn(ctx context.Context) { |
| 34 | logger.Debug("receive-packets-in-started") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 35 | // If we exit, assume disconnected |
| 36 | defer func() { |
| 37 | ofa.events <- ofaEventVolthaDisconnected |
| 38 | logger.Debug("receive-packets-in-finished") |
| 39 | }() |
| 40 | if ofa.volthaClient == nil { |
| 41 | logger.Error("no-voltha-connection") |
| 42 | return |
| 43 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 44 | opt := grpc.EmptyCallOption{} |
| 45 | streamCtx, streamDone := context.WithCancel(context.Background()) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 46 | defer streamDone() |
David Bainbridge | 20e34e7 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 47 | stream, err := ofa.volthaClient.Get().ReceivePacketsIn(streamCtx, &empty.Empty{}, opt) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 48 | if err != nil { |
| 49 | logger.Errorw("Unable to establish Receive PacketIn Stream", |
| 50 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 51 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 52 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 53 | |
| 54 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 55 | |
| 56 | for { |
| 57 | select { |
| 58 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 59 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 60 | default: |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 61 | pkt, err := stream.Recv() |
| 62 | if err != nil { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 63 | logger.Errorw("error receiving packet", |
| 64 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 65 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 66 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 67 | ofa.packetInChannel <- pkt |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func (ofa *OFAgent) handlePacketsIn(ctx context.Context) { |
| 73 | logger.Debug("handle-packets-in-started") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 74 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 75 | for { |
| 76 | select { |
| 77 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 78 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 79 | case packet := <-ofa.packetInChannel: |
| 80 | packetIn := packet.GetPacketIn() |
| 81 | |
| 82 | if logger.V(log.DebugLevel) { |
| 83 | js, _ := json.Marshal(packetIn) |
| 84 | logger.Debugw("packet-in recieved", log.Fields{"packet-in": js}) |
| 85 | } |
| 86 | deviceID := packet.GetId() |
| 87 | ofPacketIn := ofp.NewPacketIn() |
| 88 | ofPacketIn.SetVersion(uint8(4)) |
| 89 | ofPacketIn.SetXid(openflow.GetXid()) |
| 90 | ofPacketIn.SetBufferId(packetIn.GetBufferId()) |
| 91 | ofPacketIn.SetCookie(packetIn.GetCookie()) |
| 92 | ofPacketIn.SetData(packetIn.GetData()) |
| 93 | match := ofp.NewMatchV3() |
| 94 | inMatch := packetIn.GetMatch() |
| 95 | match.SetType(uint16(inMatch.GetType())) |
| 96 | //oxFields := inMatch.GetOxmFields() |
| 97 | var fields []goloxi.IOxm |
| 98 | size := uint16(4) |
| 99 | for _, oxmField := range inMatch.GetOxmFields() { |
| 100 | /* |
| 101 | for i := 0; i < len(oxFields); i++ { |
| 102 | oxmField := oxFields[i] |
| 103 | */ |
| 104 | field := oxmField.GetField() |
| 105 | ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField |
| 106 | size += 4 //header for oxm |
| 107 | switch ofbField.Type { |
| 108 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT: |
| 109 | ofpInPort := ofp.NewOxmInPort() |
| 110 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port) |
| 111 | ofpInPort.Value = ofp.Port(val.Port) |
| 112 | size += 4 |
| 113 | fields = append(fields, ofpInPort) |
| 114 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE: |
| 115 | ofpEthType := ofp.NewOxmEthType() |
| 116 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType) |
| 117 | ofpEthType.Value = ofp.EthernetType(val.EthType) |
| 118 | size += 2 |
| 119 | fields = append(fields, ofpEthType) |
| 120 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT: |
| 121 | ofpInPhyPort := ofp.NewOxmInPhyPort() |
| 122 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort) |
| 123 | ofpInPhyPort.Value = ofp.Port(val.PhysicalPort) |
| 124 | size += 4 |
| 125 | fields = append(fields, ofpInPhyPort) |
| 126 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO: |
| 127 | ofpIpProto := ofp.NewOxmIpProto() |
| 128 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto) |
| 129 | ofpIpProto.Value = ofp.IpPrototype(val.IpProto) |
| 130 | size += 1 |
| 131 | fields = append(fields, ofpIpProto) |
| 132 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC: |
| 133 | ofpUdpSrc := ofp.NewOxmUdpSrc() |
| 134 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc) |
| 135 | ofpUdpSrc.Value = uint16(val.UdpSrc) |
| 136 | size += 2 |
| 137 | fields = append(fields, ofpUdpSrc) |
| 138 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST: |
| 139 | ofpUdpDst := ofp.NewOxmUdpDst() |
| 140 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst) |
| 141 | ofpUdpDst.Value = uint16(val.UdpDst) |
| 142 | size += 2 |
| 143 | fields = append(fields, ofpUdpDst) |
| 144 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID: |
| 145 | ofpVlanVid := ofp.NewOxmVlanVid() |
| 146 | val := ofbField.GetValue() |
| 147 | if val != nil { |
| 148 | vlanId := val.(*openflow_13.OfpOxmOfbField_VlanVid) |
| 149 | ofpVlanVid.Value = uint16(vlanId.VlanVid) + 0x1000 |
| 150 | size += 2 |
| 151 | } else { |
| 152 | ofpVlanVid.Value = uint16(0) |
| 153 | } |
| 154 | |
| 155 | fields = append(fields, ofpVlanVid) |
| 156 | default: |
| 157 | logger.Warnw("receive-packet-in:unhandled-oxm-field", |
| 158 | log.Fields{"field": ofbField.Type}) |
| 159 | } |
| 160 | } |
| 161 | match.SetLength(size) |
| 162 | |
| 163 | match.SetOxmList(fields) |
| 164 | |
| 165 | ofPacketIn.SetMatch(*match) |
| 166 | ofPacketIn.SetReason(uint8(packetIn.GetReason())) |
| 167 | ofPacketIn.SetTableId(uint8(packetIn.GetTableId())) |
| 168 | ofPacketIn.SetTotalLen(uint16(len(ofPacketIn.GetData()))) |
| 169 | ofc := ofa.getOFClient(deviceID) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 170 | if err := ofc.SendMessage(ofPacketIn); err != nil { |
| 171 | logger.Errorw("send-message-failed", log.Fields{ |
| 172 | "device-id": deviceID, |
| 173 | "error": err}) |
| 174 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 175 | |
| 176 | } |
| 177 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 178 | logger.Debug("handle-packets-in-finished") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 179 | } |