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