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