Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 event |
| 18 | |
| 19 | import ( |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 20 | "context" |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame^] | 21 | "encoding/binary" |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 22 | "encoding/hex" |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 23 | "sync" |
| 24 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes/empty" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 27 | "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 28 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type Manager struct { |
| 32 | packetInQueue chan openflow_13.PacketIn |
| 33 | packetInQueueDone chan bool |
| 34 | changeEventQueue chan openflow_13.ChangeEvent |
| 35 | changeEventQueueDone chan bool |
| 36 | } |
| 37 | |
| 38 | func NewManager() *Manager { |
| 39 | return &Manager{ |
| 40 | packetInQueue: make(chan openflow_13.PacketIn, 100), |
| 41 | packetInQueueDone: make(chan bool, 1), |
| 42 | changeEventQueue: make(chan openflow_13.ChangeEvent, 100), |
| 43 | changeEventQueueDone: make(chan bool, 1), |
| 44 | } |
| 45 | } |
| 46 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 47 | func (q *Manager) SendPacketIn(ctx context.Context, deviceID string, transationID string, packet *openflow_13.OfpPacketIn) { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 48 | // TODO: Augment the OF PacketIn to include the transactionId |
| 49 | packetIn := openflow_13.PacketIn{Id: deviceID, PacketIn: packet} |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 50 | logger.Debugw(ctx, "SendPacketIn", log.Fields{"packetIn": packetIn}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 51 | q.packetInQueue <- packetIn |
| 52 | } |
| 53 | |
| 54 | type callTracker struct { |
| 55 | failedPacket interface{} |
| 56 | } |
| 57 | type streamTracker struct { |
| 58 | calls map[string]*callTracker |
| 59 | sync.Mutex |
| 60 | } |
| 61 | |
| 62 | var streamingTracker = &streamTracker{calls: make(map[string]*callTracker)} |
| 63 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 64 | func (q *Manager) getStreamingTracker(ctx context.Context, method string, done chan<- bool) *callTracker { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 65 | streamingTracker.Lock() |
| 66 | defer streamingTracker.Unlock() |
| 67 | if _, ok := streamingTracker.calls[method]; ok { |
| 68 | // bail out the other packet in thread |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 69 | logger.Debugf(ctx, "%s streaming call already running. Exiting it", method) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 70 | done <- true |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 71 | logger.Debugf(ctx, "Last %s exited. Continuing ...", method) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 72 | } else { |
| 73 | streamingTracker.calls[method] = &callTracker{failedPacket: nil} |
| 74 | } |
| 75 | return streamingTracker.calls[method] |
| 76 | } |
| 77 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 78 | func (q *Manager) flushFailedPackets(ctx context.Context, tracker *callTracker) error { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 79 | if tracker.failedPacket != nil { |
| 80 | switch tracker.failedPacket.(type) { |
| 81 | case openflow_13.PacketIn: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 82 | logger.Debug(ctx, "Enqueueing last failed packetIn") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 83 | q.packetInQueue <- tracker.failedPacket.(openflow_13.PacketIn) |
| 84 | case openflow_13.ChangeEvent: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 85 | logger.Debug(ctx, "Enqueueing last failed changeEvent") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 86 | q.changeEventQueue <- tracker.failedPacket.(openflow_13.ChangeEvent) |
| 87 | } |
| 88 | } |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | // ReceivePacketsIn receives packets from adapter |
| 93 | func (q *Manager) ReceivePacketsIn(_ *empty.Empty, packetsIn voltha.VolthaService_ReceivePacketsInServer) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 94 | ctx := context.Background() |
| 95 | var streamingTracker = q.getStreamingTracker(ctx, "ReceivePacketsIn", q.packetInQueueDone) |
| 96 | logger.Debugw(ctx, "ReceivePacketsIn-request", log.Fields{"packetsIn": packetsIn}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 97 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 98 | err := q.flushFailedPackets(ctx, streamingTracker) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 99 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 100 | logger.Errorw(ctx, "unable-to-flush-failed-packets", log.Fields{"error": err}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | loop: |
| 104 | for { |
| 105 | select { |
| 106 | case packet := <-q.packetInQueue: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 107 | logger.Debugw(ctx, "sending-packet-in", log.Fields{ |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 108 | "packet": hex.EncodeToString(packet.PacketIn.Data), |
| 109 | }) |
| 110 | if err := packetsIn.Send(&packet); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 111 | logger.Errorw(ctx, "failed-to-send-packet", log.Fields{"error": err}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 112 | // save the last failed packet in |
| 113 | streamingTracker.failedPacket = packet |
| 114 | } else { |
| 115 | if streamingTracker.failedPacket != nil { |
| 116 | // reset last failed packet saved to avoid flush |
| 117 | streamingTracker.failedPacket = nil |
| 118 | } |
| 119 | } |
| 120 | case <-q.packetInQueueDone: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 121 | logger.Debug(ctx, "Another ReceivePacketsIn running. Bailing out ...") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 122 | break loop |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | //TODO: Find an elegant way to get out of the above loop when the Core is stopped |
| 127 | return nil |
| 128 | } |
| 129 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 130 | func (q *Manager) SendChangeEvent(ctx context.Context, deviceID string, reason openflow_13.OfpPortReason, desc *openflow_13.OfpPort) { |
| 131 | logger.Debugw(ctx, "SendChangeEvent", log.Fields{"device-id": deviceID, "reason": reason, "desc": desc}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 132 | q.changeEventQueue <- openflow_13.ChangeEvent{ |
| 133 | Id: deviceID, |
| 134 | Event: &openflow_13.ChangeEvent_PortStatus{ |
| 135 | PortStatus: &openflow_13.OfpPortStatus{ |
| 136 | Reason: reason, |
| 137 | Desc: desc, |
| 138 | }, |
| 139 | }, |
| 140 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame^] | 143 | func (q *Manager) SendFlowChangeEvent(ctx context.Context, deviceID string, res []error, xid uint32, flowCookie uint64) { |
| 144 | logger.Debugw(ctx, "SendChangeEvent", log.Fields{"device-id": deviceID, |
| 145 | "flowId": xid, "flowCookie": flowCookie, "errors": res}) |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 146 | errorType := openflow_13.OfpErrorType_OFPET_FLOW_MOD_FAILED |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame^] | 147 | //Manually creating the data payload for the flow error message |
| 148 | bs := make([]byte, 2) |
| 149 | //OF 1.3 |
| 150 | bs[0] = byte(4) |
| 151 | //Flow Mod |
| 152 | bs[1] = byte(14) |
| 153 | //Length of the message |
| 154 | length := make([]byte, 2) |
| 155 | binary.BigEndian.PutUint16(length, 56) |
| 156 | bs = append(bs, length...) |
| 157 | emptyArr := []byte{0, 0, 0, 0} |
| 158 | bs = append(bs, emptyArr...) |
| 159 | //Cookie of the Flow |
| 160 | cookie := make([]byte, 52) |
| 161 | binary.BigEndian.PutUint64(cookie, flowCookie) |
| 162 | bs = append(bs, cookie...) |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 163 | q.changeEventQueue <- openflow_13.ChangeEvent{ |
| 164 | Id: deviceID, |
| 165 | Event: &openflow_13.ChangeEvent_Error{ |
| 166 | Error: &openflow_13.OfpErrorMsg{ |
| 167 | Header: &openflow_13.OfpHeader{ |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame^] | 168 | Type: openflow_13.OfpType_OFPT_FLOW_MOD, |
| 169 | Xid: xid, |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 170 | }, |
| 171 | Type: uint32(errorType), |
| 172 | Code: uint32(openflow_13.OfpFlowModFailedCode_OFPFMFC_UNKNOWN), |
Andrea Campanella | 4afb2f0 | 2021-01-29 09:38:57 +0100 | [diff] [blame^] | 173 | Data: bs, |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 174 | }, |
| 175 | }, |
| 176 | } |
| 177 | } |
| 178 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 179 | // ReceiveChangeEvents receives change in events |
| 180 | func (q *Manager) ReceiveChangeEvents(_ *empty.Empty, changeEvents voltha.VolthaService_ReceiveChangeEventsServer) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 181 | ctx := context.Background() |
| 182 | var streamingTracker = q.getStreamingTracker(ctx, "ReceiveChangeEvents", q.changeEventQueueDone) |
| 183 | logger.Debugw(ctx, "ReceiveChangeEvents-request", log.Fields{"changeEvents": changeEvents}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 184 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 185 | err := q.flushFailedPackets(ctx, streamingTracker) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 186 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 187 | logger.Errorw(ctx, "unable-to-flush-failed-packets", log.Fields{"error": err}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | loop: |
| 191 | for { |
| 192 | select { |
| 193 | // Dequeue a change event |
| 194 | case event := <-q.changeEventQueue: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 195 | logger.Debugw(ctx, "sending-change-event", log.Fields{"event": event}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 196 | if err := changeEvents.Send(&event); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 197 | logger.Errorw(ctx, "failed-to-send-change-event", log.Fields{"error": err}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 198 | // save last failed changeevent |
| 199 | streamingTracker.failedPacket = event |
| 200 | } else { |
| 201 | if streamingTracker.failedPacket != nil { |
| 202 | // reset last failed event saved on success to avoid flushing |
| 203 | streamingTracker.failedPacket = nil |
| 204 | } |
| 205 | } |
| 206 | case <-q.changeEventQueueDone: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 207 | logger.Debug(ctx, "Another ReceiveChangeEvents already running. Bailing out ...") |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 208 | break loop |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | func (q *Manager) GetChangeEventsQueueForTest() <-chan openflow_13.ChangeEvent { |
| 216 | return q.changeEventQueue |
| 217 | } |