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 | f8ce7d2 | 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/golang/protobuf/ptypes/empty" |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 24 | "github.com/opencord/goloxi" |
| 25 | ofp "github.com/opencord/goloxi/of13" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 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) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 34 | logger.Debug(ctx, "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 |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 38 | logger.Debug(ctx, "receive-packets-in-finished") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 39 | }() |
| 40 | if ofa.volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 41 | logger.Error(ctx, "no-voltha-connection") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 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 | f8ce7d2 | 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 { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 49 | logger.Errorw(ctx, "Unable to establish Receive PacketIn Stream", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 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 { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 63 | logger.Errorw(ctx, "error receiving packet", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 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) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 73 | logger.Debug(ctx, "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) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 84 | logger.Debugw(ctx, "packet-in received", log.Fields{"packet-in": js}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 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())) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 96 | var fields []goloxi.IOxm |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 97 | for _, oxmField := range inMatch.GetOxmFields() { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 98 | field := oxmField.GetField() |
| 99 | ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 100 | switch ofbField.Type { |
| 101 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT: |
| 102 | ofpInPort := ofp.NewOxmInPort() |
| 103 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port) |
| 104 | ofpInPort.Value = ofp.Port(val.Port) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 105 | fields = append(fields, ofpInPort) |
| 106 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE: |
| 107 | ofpEthType := ofp.NewOxmEthType() |
| 108 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType) |
| 109 | ofpEthType.Value = ofp.EthernetType(val.EthType) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 110 | fields = append(fields, ofpEthType) |
| 111 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT: |
| 112 | ofpInPhyPort := ofp.NewOxmInPhyPort() |
| 113 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort) |
| 114 | ofpInPhyPort.Value = ofp.Port(val.PhysicalPort) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 115 | fields = append(fields, ofpInPhyPort) |
| 116 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO: |
| 117 | ofpIpProto := ofp.NewOxmIpProto() |
| 118 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto) |
| 119 | ofpIpProto.Value = ofp.IpPrototype(val.IpProto) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 120 | fields = append(fields, ofpIpProto) |
| 121 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC: |
| 122 | ofpUdpSrc := ofp.NewOxmUdpSrc() |
| 123 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc) |
| 124 | ofpUdpSrc.Value = uint16(val.UdpSrc) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 125 | fields = append(fields, ofpUdpSrc) |
| 126 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST: |
| 127 | ofpUdpDst := ofp.NewOxmUdpDst() |
| 128 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst) |
| 129 | ofpUdpDst.Value = uint16(val.UdpDst) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 130 | fields = append(fields, ofpUdpDst) |
| 131 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID: |
| 132 | ofpVlanVid := ofp.NewOxmVlanVid() |
| 133 | val := ofbField.GetValue() |
| 134 | if val != nil { |
| 135 | vlanId := val.(*openflow_13.OfpOxmOfbField_VlanVid) |
| 136 | ofpVlanVid.Value = uint16(vlanId.VlanVid) + 0x1000 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 137 | } else { |
| 138 | ofpVlanVid.Value = uint16(0) |
| 139 | } |
| 140 | |
| 141 | fields = append(fields, ofpVlanVid) |
| 142 | default: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 143 | logger.Warnw(ctx, "receive-packet-in:unhandled-oxm-field", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 144 | log.Fields{"field": ofbField.Type}) |
| 145 | } |
| 146 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 147 | |
| 148 | match.SetOxmList(fields) |
| 149 | |
| 150 | ofPacketIn.SetMatch(*match) |
| 151 | ofPacketIn.SetReason(uint8(packetIn.GetReason())) |
| 152 | ofPacketIn.SetTableId(uint8(packetIn.GetTableId())) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 153 | ofc := ofa.getOFClient(ctx, deviceID) |
| 154 | if err := ofc.SendMessage(ctx, ofPacketIn); err != nil { |
| 155 | logger.Errorw(ctx, "send-message-failed", log.Fields{ |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 156 | "device-id": deviceID, |
| 157 | "error": err}) |
| 158 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 159 | |
| 160 | } |
| 161 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 162 | logger.Debug(ctx, "handle-packets-in-finished") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 163 | } |