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 openflow |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
Kent Hagerman | 9b0ac0a | 2020-06-08 11:48:16 -0400 | [diff] [blame] | 22 | "net" |
| 23 | |
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 | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 27 | "github.com/opencord/voltha-protos/v5/go/common" |
| 28 | "github.com/opencord/voltha-protos/v5/go/openflow_13" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 31 | func (ofc *OFConnection) handleStatsRequest(ctx context.Context, request ofp.IHeader, statType uint16) error { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 32 | span, ctx := log.CreateChildSpan(ctx, "openflow-stats") |
| 33 | defer span.Finish() |
| 34 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 35 | if logger.V(log.DebugLevel) { |
| 36 | js, _ := json.Marshal(request) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 37 | logger.Debugw(ctx, "handleStatsRequest called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 38 | log.Fields{ |
| 39 | "device-id": ofc.DeviceID, |
| 40 | "stat-type": statType, |
| 41 | "request": js}) |
| 42 | } |
| 43 | |
| 44 | switch statType { |
| 45 | case ofp.OFPSTDesc: |
| 46 | statsReq := request.(*ofp.DescStatsRequest) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 47 | response, err := ofc.handleDescStatsRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | if logger.V(log.DebugLevel) { |
| 52 | reqJs, _ := json.Marshal(statsReq) |
| 53 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 54 | logger.Debugw(ctx, "handle-stats-request-desc", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 55 | log.Fields{ |
| 56 | "device-id": ofc.DeviceID, |
| 57 | "request": reqJs, |
| 58 | "response": resJs}) |
| 59 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 60 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 61 | case ofp.OFPSTFlow: |
| 62 | statsReq := request.(*ofp.FlowStatsRequest) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 63 | responses, err := ofc.handleFlowStatsRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 64 | if err != nil { |
| 65 | return err |
| 66 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 67 | if logger.V(log.DebugLevel) { |
| 68 | reqJs, _ := json.Marshal(statsReq) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 69 | resJs, _ := json.Marshal(responses) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 70 | logger.Debugw(ctx, "handle-stats-request-flow", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 71 | log.Fields{ |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 72 | "device-id": ofc.DeviceID, |
| 73 | "request": reqJs, |
| 74 | "responses-object": responses, |
| 75 | "response": resJs}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 76 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 77 | for _, response := range responses { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 78 | err := ofc.SendMessage(ctx, response) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | } |
| 83 | return nil |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 84 | |
| 85 | case ofp.OFPSTAggregate: |
| 86 | statsReq := request.(*ofp.AggregateStatsRequest) |
| 87 | response, err := ofc.handleAggregateStatsRequest(statsReq) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if logger.V(log.DebugLevel) { |
| 92 | reqJs, _ := json.Marshal(statsReq) |
| 93 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 94 | logger.Debugw(ctx, "handle-stats-request-aggregate", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 95 | log.Fields{ |
| 96 | "device-id": ofc.DeviceID, |
| 97 | "request": reqJs, |
| 98 | "response": resJs}) |
| 99 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 100 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 101 | case ofp.OFPSTTable: |
| 102 | statsReq := request.(*ofp.TableStatsRequest) |
| 103 | response, e := ofc.handleTableStatsRequest(statsReq) |
| 104 | if logger.V(log.DebugLevel) { |
| 105 | reqJs, _ := json.Marshal(statsReq) |
| 106 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 107 | logger.Debugw(ctx, "handle-stats-request-table", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 108 | log.Fields{ |
| 109 | "device-id": ofc.DeviceID, |
| 110 | "request": reqJs, |
| 111 | "response": resJs}) |
| 112 | } |
| 113 | if e != nil { |
| 114 | return e |
| 115 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 116 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 117 | case ofp.OFPSTPort: |
| 118 | statsReq := request.(*ofp.PortStatsRequest) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 119 | responses, err := ofc.handlePortStatsRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | if logger.V(log.DebugLevel) { |
| 124 | reqJs, _ := json.Marshal(statsReq) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 125 | resJs, _ := json.Marshal(responses) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 126 | logger.Debugw(ctx, "handle-stats-request-port", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 127 | log.Fields{ |
| 128 | "device-id": ofc.DeviceID, |
| 129 | "request": reqJs, |
| 130 | "response": resJs}) |
| 131 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 132 | for _, response := range responses { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 133 | err := ofc.SendMessage(ctx, response) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | } |
| 138 | return nil |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 139 | case ofp.OFPSTQueue: |
| 140 | statsReq := request.(*ofp.QueueStatsRequest) |
| 141 | response, err := ofc.handleQueueStatsRequest(statsReq) |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | if logger.V(log.DebugLevel) { |
| 146 | reqJs, _ := json.Marshal(statsReq) |
| 147 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 148 | logger.Debugw(ctx, "handle-stats-request-queue", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 149 | log.Fields{ |
| 150 | "device-id": ofc.DeviceID, |
| 151 | "request": reqJs, |
| 152 | "response": resJs}) |
| 153 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 154 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 155 | case ofp.OFPSTGroup: |
| 156 | statsReq := request.(*ofp.GroupStatsRequest) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 157 | response, err := ofc.handleGroupStatsRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | if logger.V(log.DebugLevel) { |
| 162 | reqJs, _ := json.Marshal(statsReq) |
| 163 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 164 | logger.Debugw(ctx, "handle-stats-request-group", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 165 | log.Fields{ |
| 166 | "device-id": ofc.DeviceID, |
| 167 | "request": reqJs, |
| 168 | "response": resJs}) |
| 169 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 170 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 171 | case ofp.OFPSTGroupDesc: |
| 172 | statsReq := request.(*ofp.GroupDescStatsRequest) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 173 | response, err := ofc.handleGroupStatsDescRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | if logger.V(log.DebugLevel) { |
| 178 | reqJs, _ := json.Marshal(statsReq) |
| 179 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 180 | logger.Debugw(ctx, "handle-stats-request-group-desc", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 181 | log.Fields{ |
| 182 | "device-id": ofc.DeviceID, |
| 183 | "request": reqJs, |
| 184 | "response": resJs}) |
| 185 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 186 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 187 | |
| 188 | case ofp.OFPSTGroupFeatures: |
| 189 | statsReq := request.(*ofp.GroupFeaturesStatsRequest) |
| 190 | response, err := ofc.handleGroupFeatureStatsRequest(statsReq) |
| 191 | if err != nil { |
| 192 | return err |
| 193 | } |
| 194 | if logger.V(log.DebugLevel) { |
| 195 | reqJs, _ := json.Marshal(statsReq) |
| 196 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 197 | logger.Debugw(ctx, "handle-stats-request-group-features", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 198 | log.Fields{ |
| 199 | "device-id": ofc.DeviceID, |
| 200 | "request": reqJs, |
| 201 | "response": resJs}) |
| 202 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 203 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 204 | case ofp.OFPSTMeter: |
| 205 | statsReq := request.(*ofp.MeterStatsRequest) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 206 | response, err := ofc.handleMeterStatsRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 207 | if err != nil { |
| 208 | return err |
| 209 | } |
| 210 | if logger.V(log.DebugLevel) { |
| 211 | reqJs, _ := json.Marshal(statsReq) |
| 212 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 213 | logger.Debugw(ctx, "handle-stats-request-meter", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 214 | log.Fields{ |
| 215 | "device-id": ofc.DeviceID, |
| 216 | "request": reqJs, |
| 217 | "response": resJs}) |
| 218 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 219 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 220 | case ofp.OFPSTMeterConfig: |
| 221 | statsReq := request.(*ofp.MeterConfigStatsRequest) |
| 222 | response, err := ofc.handleMeterConfigStatsRequest(statsReq) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | if logger.V(log.DebugLevel) { |
| 227 | reqJs, _ := json.Marshal(statsReq) |
| 228 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 229 | logger.Debugw(ctx, "handle-stats-request-meter-config", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 230 | log.Fields{ |
| 231 | "device-id": ofc.DeviceID, |
| 232 | "request": reqJs, |
| 233 | "response": resJs}) |
| 234 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 235 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 236 | case ofp.OFPSTMeterFeatures: |
| 237 | statsReq := request.(*ofp.MeterFeaturesStatsRequest) |
| 238 | response, err := ofc.handleMeterFeatureStatsRequest(statsReq) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
| 242 | if logger.V(log.DebugLevel) { |
| 243 | reqJs, _ := json.Marshal(statsReq) |
| 244 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 245 | logger.Debugw(ctx, "handle-stats-request-meter-features", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 246 | log.Fields{ |
| 247 | "device-id": ofc.DeviceID, |
| 248 | "request": reqJs, |
| 249 | "response": resJs}) |
| 250 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 251 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 252 | case ofp.OFPSTTableFeatures: |
| 253 | statsReq := request.(*ofp.TableFeaturesStatsRequest) |
| 254 | response, err := ofc.handleTableFeaturesStatsRequest(statsReq) |
| 255 | if err != nil { |
| 256 | return err |
| 257 | } |
| 258 | if logger.V(log.DebugLevel) { |
| 259 | reqJs, _ := json.Marshal(statsReq) |
| 260 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 261 | logger.Debugw(ctx, "handle-stats-request-table-features", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 262 | log.Fields{ |
| 263 | "device-id": ofc.DeviceID, |
| 264 | "request": reqJs, |
| 265 | "response": resJs}) |
| 266 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 267 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 268 | case ofp.OFPSTPortDesc: |
| 269 | statsReq := request.(*ofp.PortDescStatsRequest) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 270 | responses, err := ofc.handlePortDescStatsRequest(ctx, statsReq) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | if logger.V(log.DebugLevel) { |
| 275 | reqJs, _ := json.Marshal(statsReq) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 276 | resJs, _ := json.Marshal(responses) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 277 | logger.Debugw(ctx, "handle-stats-request-port-desc", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 278 | log.Fields{ |
| 279 | "device-id": ofc.DeviceID, |
| 280 | "request": reqJs, |
| 281 | "response": resJs}) |
| 282 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 283 | for _, response := range responses { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 284 | err := ofc.SendMessage(ctx, response) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 285 | if err != nil { |
| 286 | return err |
| 287 | } |
| 288 | } |
| 289 | return nil |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 290 | |
| 291 | case ofp.OFPSTExperimenter: |
| 292 | statsReq := request.(*ofp.ExperimenterStatsRequest) |
| 293 | response, err := ofc.handleExperimenterStatsRequest(statsReq) |
| 294 | if err != nil { |
| 295 | return err |
| 296 | } |
| 297 | if logger.V(log.DebugLevel) { |
| 298 | reqJs, _ := json.Marshal(statsReq) |
| 299 | resJs, _ := json.Marshal(response) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 300 | logger.Debugw(ctx, "handle-stats-request-experimenter", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 301 | log.Fields{ |
| 302 | "device-id": ofc.DeviceID, |
| 303 | "request": reqJs, |
| 304 | "response": resJs}) |
| 305 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 306 | return ofc.SendMessage(ctx, response) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 307 | } |
| 308 | return nil |
| 309 | } |
| 310 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 311 | func (ofc *OFConnection) handleDescStatsRequest(ctx context.Context, request *ofp.DescStatsRequest) (*ofp.DescStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 312 | volthaClient := ofc.VolthaClient.Get() |
| 313 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 314 | return nil, NoVolthaConnectionError |
| 315 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 316 | response := ofp.NewDescStatsReply() |
| 317 | response.SetXid(request.GetXid()) |
| 318 | response.SetVersion(request.GetVersion()) |
| 319 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 320 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 321 | resp, err := volthaClient.GetLogicalDevice(log.WithSpanFromContext(context.Background(), ctx), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 322 | &common.ID{Id: ofc.DeviceID}) |
| 323 | if err != nil { |
| 324 | return nil, err |
| 325 | } |
| 326 | desc := resp.GetDesc() |
| 327 | |
| 328 | response.SetMfrDesc(PadString(desc.GetMfrDesc(), 256)) |
| 329 | response.SetHwDesc(PadString(desc.GetHwDesc(), 256)) |
| 330 | response.SetSwDesc(PadString(desc.GetSwDesc(), 256)) |
| 331 | response.SetSerialNum(PadString(desc.GetSerialNum(), 32)) |
| 332 | response.SetDpDesc(PadString(desc.GetDpDesc(), 256)) |
| 333 | return response, nil |
| 334 | } |
| 335 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 336 | func (ofc *OFConnection) handleFlowStatsRequest(ctx context.Context, request *ofp.FlowStatsRequest) ([]*ofp.FlowStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 337 | volthaClient := ofc.VolthaClient.Get() |
| 338 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 339 | return nil, NoVolthaConnectionError |
| 340 | } |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 341 | resp, err := volthaClient.ListLogicalDeviceFlows(log.WithSpanFromContext(context.Background(), ctx), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 342 | &common.ID{Id: ofc.DeviceID}) |
| 343 | if err != nil { |
| 344 | return nil, err |
| 345 | } |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 346 | logger.Debugw(ctx, "logicalDeviceFlows", log.Fields{"logical-device-id": ofc.DeviceID, "num-flows": len(resp.Items)}) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 347 | var flows []*ofp.FlowStatsEntry |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 348 | for _, item := range resp.GetItems() { |
| 349 | entry := ofp.NewFlowStatsEntry() |
| 350 | entry.SetTableId(uint8(item.GetTableId())) |
| 351 | entry.SetDurationSec(item.GetDurationSec()) |
| 352 | entry.SetDurationNsec(item.GetDurationNsec()) |
| 353 | entry.SetPriority(uint16(item.GetPriority())) |
| 354 | entry.SetIdleTimeout(uint16(item.GetIdleTimeout())) |
| 355 | entry.SetHardTimeout(uint16(item.GetHardTimeout())) |
| 356 | entry.SetFlags(ofp.FlowModFlags(item.GetFlags())) |
| 357 | entry.SetCookie(item.GetCookie()) |
| 358 | entry.SetPacketCount(item.GetPacketCount()) |
| 359 | entry.SetByteCount(item.GetByteCount()) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 360 | match := ofp.NewMatchV3() |
| 361 | pbMatch := item.GetMatch() |
| 362 | match.SetType(uint16(pbMatch.GetType())) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 363 | var fields []goloxi.IOxm |
| 364 | for _, oxmField := range pbMatch.GetOxmFields() { |
| 365 | field := oxmField.GetField() |
| 366 | ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame] | 367 | iOxm, err := parseOxm(ctx, ofbField) |
| 368 | if err == nil { |
| 369 | fields = append(fields, iOxm) |
| 370 | } else { |
| 371 | logger.Errorw(ctx, "error-parsing-oxm", log.Fields{"err": err}) |
| 372 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | match.OxmList = fields |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 376 | entry.SetMatch(*match) |
| 377 | var instructions []ofp.IInstruction |
| 378 | for _, ofpInstruction := range item.Instructions { |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame] | 379 | instruction, err := parseInstructions(ctx, ofpInstruction) |
| 380 | if err == nil { |
| 381 | instructions = append(instructions, instruction) |
| 382 | } else { |
| 383 | logger.Errorw(ctx, "error-parsing-instruction", log.Fields{"err": err}) |
| 384 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 385 | } |
| 386 | entry.Instructions = instructions |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 387 | flows = append(flows, entry) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 388 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 389 | var responses []*ofp.FlowStatsReply |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 390 | |
Matteo Scandolo | 256266d | 2020-06-01 13:44:07 -0700 | [diff] [blame] | 391 | chunkSize := ofc.flowsChunkSize |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 392 | total := len(flows) / chunkSize |
| 393 | n := 0 |
| 394 | for n <= total { |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 395 | response := ofp.NewFlowStatsReply() |
| 396 | response.SetXid(request.GetXid()) |
| 397 | response.SetVersion(request.GetVersion()) |
| 398 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 399 | |
Andrea Campanella | f2cf13e | 2020-09-03 16:22:27 +0200 | [diff] [blame] | 400 | limit := (n * chunkSize) + chunkSize |
Andrea Campanella | f2cf13e | 2020-09-03 16:22:27 +0200 | [diff] [blame] | 401 | chunk := flows[n*chunkSize : min(limit, len(flows))] |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 402 | |
| 403 | if len(chunk) == 0 { |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 404 | // Special case - no flows |
| 405 | if len(flows) == 0 { |
| 406 | logger.Debugw(ctx, "no-flows-present", log.Fields{"logical-device-id": ofc.DeviceID}) |
| 407 | response.SetEntries(chunk) |
| 408 | responses = append(responses, response) |
| 409 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 410 | break |
| 411 | } |
| 412 | |
Andrea Campanella | f2cf13e | 2020-09-03 16:22:27 +0200 | [diff] [blame] | 413 | if limit < len(flows) { |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 414 | response.SetFlags(ofp.StatsReplyFlags(ofp.OFPSFReplyMore)) |
| 415 | } |
| 416 | response.SetEntries(chunk) |
| 417 | responses = append(responses, response) |
| 418 | n++ |
| 419 | } |
| 420 | return responses, nil |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 423 | func (ofc *OFConnection) handleAggregateStatsRequest(request *ofp.AggregateStatsRequest) (*ofp.AggregateStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 424 | response := ofp.NewAggregateStatsReply() |
| 425 | response.SetVersion(request.GetVersion()) |
| 426 | response.SetXid(request.GetXid()) |
| 427 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 428 | response.SetFlowCount(0) |
| 429 | //TODO wire this to voltha core when it implements |
| 430 | return response, nil |
| 431 | } |
| 432 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 433 | func (ofc *OFConnection) handleGroupStatsRequest(ctx context.Context, request *ofp.GroupStatsRequest) (*ofp.GroupStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 434 | volthaClient := ofc.VolthaClient.Get() |
| 435 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 436 | return nil, NoVolthaConnectionError |
| 437 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 438 | response := ofp.NewGroupStatsReply() |
| 439 | response.SetVersion(request.GetVersion()) |
| 440 | response.SetXid(request.GetXid()) |
| 441 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 442 | reply, err := volthaClient.ListLogicalDeviceFlowGroups(log.WithSpanFromContext(context.Background(), ctx), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 443 | &common.ID{Id: ofc.DeviceID}) |
| 444 | if err != nil { |
| 445 | return nil, err |
| 446 | } |
| 447 | |
| 448 | var groupStatsEntries []*ofp.GroupStatsEntry |
| 449 | for _, item := range reply.GetItems() { |
| 450 | stats := item.GetStats() |
| 451 | var entry ofp.GroupStatsEntry |
| 452 | entry.SetByteCount(stats.GetByteCount()) |
| 453 | entry.SetPacketCount(stats.GetPacketCount()) |
| 454 | entry.SetDurationNsec(stats.GetDurationNsec()) |
| 455 | entry.SetDurationSec(stats.GetDurationSec()) |
| 456 | entry.SetRefCount(stats.GetRefCount()) |
| 457 | entry.SetGroupId(stats.GetGroupId()) |
| 458 | var bucketStatsList []*ofp.BucketCounter |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 459 | // TODO fix this when API handler is fixed in the core |
| 460 | // Core doesn't return any buckets in the Stats object, so just |
| 461 | // fill out an empty BucketCounter for each bucket in the group Desc for now. |
| 462 | for range item.GetDesc().GetBuckets() { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 463 | bucketCounter := ofp.BucketCounter{} |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 464 | bucketCounter.SetPacketCount(0) |
| 465 | bucketCounter.SetByteCount(0) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 466 | bucketStatsList = append(bucketStatsList, &bucketCounter) |
| 467 | } |
| 468 | entry.SetBucketStats(bucketStatsList) |
| 469 | groupStatsEntries = append(groupStatsEntries, &entry) |
| 470 | } |
| 471 | response.SetEntries(groupStatsEntries) |
| 472 | return response, nil |
| 473 | } |
| 474 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 475 | func (ofc *OFConnection) handleGroupStatsDescRequest(ctx context.Context, request *ofp.GroupDescStatsRequest) (*ofp.GroupDescStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 476 | volthaClient := ofc.VolthaClient.Get() |
| 477 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 478 | return nil, NoVolthaConnectionError |
| 479 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 480 | response := ofp.NewGroupDescStatsReply() |
| 481 | response.SetVersion(request.GetVersion()) |
| 482 | response.SetXid(request.GetXid()) |
| 483 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 484 | reply, err := volthaClient.ListLogicalDeviceFlowGroups(log.WithSpanFromContext(context.Background(), ctx), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 485 | &common.ID{Id: ofc.DeviceID}) |
| 486 | if err != nil { |
| 487 | return nil, err |
| 488 | } |
| 489 | var groupDescStatsEntries []*ofp.GroupDescStatsEntry |
| 490 | for _, item := range reply.GetItems() { |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 491 | desc := item.GetDesc() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 492 | |
Andrea Campanella | 22aa3ed | 2021-03-04 15:58:55 +0100 | [diff] [blame] | 493 | buckets, err := volthaBucketsToOpenflow(ctx, desc.Buckets) |
| 494 | if err != nil { |
| 495 | return nil, err |
| 496 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 497 | |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 498 | groupDesc := &ofp.GroupDescStatsEntry{ |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 499 | GroupType: volthaGroupTypeToOpenflow(ctx, desc.Type), |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 500 | GroupId: desc.GroupId, |
| 501 | Buckets: buckets, |
| 502 | } |
| 503 | groupDescStatsEntries = append(groupDescStatsEntries, groupDesc) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 504 | } |
| 505 | response.SetEntries(groupDescStatsEntries) |
| 506 | return response, nil |
| 507 | } |
| 508 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 509 | func (ofc *OFConnection) handleGroupFeatureStatsRequest(request *ofp.GroupFeaturesStatsRequest) (*ofp.GroupFeaturesStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 510 | response := ofp.NewGroupFeaturesStatsReply() |
| 511 | response.SetVersion(request.GetVersion()) |
| 512 | response.SetXid(request.GetXid()) |
| 513 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 514 | //TODO wire this to voltha core when it implements |
| 515 | return response, nil |
| 516 | } |
| 517 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 518 | func (ofc *OFConnection) handleMeterStatsRequest(ctx context.Context, request *ofp.MeterStatsRequest) (*ofp.MeterStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 519 | volthaClient := ofc.VolthaClient.Get() |
| 520 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 521 | return nil, NoVolthaConnectionError |
| 522 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 523 | response := ofp.NewMeterStatsReply() |
| 524 | response.SetVersion(request.GetVersion()) |
| 525 | response.SetXid(request.GetXid()) |
| 526 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 527 | resp, err := volthaClient.ListLogicalDeviceMeters(log.WithSpanFromContext(context.Background(), ctx), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 528 | &common.ID{Id: ofc.DeviceID}) |
| 529 | if err != nil { |
| 530 | return nil, err |
| 531 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 532 | var meterStats []*ofp.MeterStats |
| 533 | for _, item := range resp.Items { |
| 534 | meterStat := ofp.NewMeterStats() |
| 535 | stats := item.Stats |
| 536 | meterStat.DurationNsec = stats.DurationNsec |
| 537 | meterStat.DurationSec = stats.DurationSec |
| 538 | meterStat.ByteInCount = stats.ByteInCount |
| 539 | meterStat.FlowCount = stats.FlowCount |
| 540 | meterStat.MeterId = stats.MeterId |
| 541 | var bandStats []*ofp.MeterBandStats |
| 542 | for _, bStat := range stats.BandStats { |
| 543 | bandStat := ofp.NewMeterBandStats() |
| 544 | bandStat.ByteBandCount = bStat.ByteBandCount |
| 545 | bandStat.PacketBandCount = bStat.PacketBandCount |
| 546 | bandStats = append(bandStats, bandStat) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 547 | } |
| 548 | meterStat.SetBandStats(bandStats) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 549 | meterStats = append(meterStats, meterStat) |
| 550 | } |
| 551 | response.SetEntries(meterStats) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 552 | return response, nil |
| 553 | } |
| 554 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 555 | func (ofc *OFConnection) handleMeterConfigStatsRequest(request *ofp.MeterConfigStatsRequest) (*ofp.MeterConfigStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 556 | response := ofp.NewMeterConfigStatsReply() |
| 557 | response.SetVersion(request.GetVersion()) |
| 558 | response.SetXid(request.GetXid()) |
| 559 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 560 | //TODO wire this to voltha core when it implements |
| 561 | return response, nil |
| 562 | } |
| 563 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 564 | func (ofc *OFConnection) handleTableFeaturesStatsRequest(request *ofp.TableFeaturesStatsRequest) (*ofp.TableFeaturesStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 565 | response := ofp.NewTableFeaturesStatsReply() |
| 566 | response.SetVersion(request.GetVersion()) |
| 567 | response.SetXid(request.GetXid()) |
| 568 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 569 | //TODO wire this to voltha core when it implements |
| 570 | return response, nil |
| 571 | } |
| 572 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 573 | func (ofc *OFConnection) handleTableStatsRequest(request *ofp.TableStatsRequest) (*ofp.TableStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 574 | var response = ofp.NewTableStatsReply() |
| 575 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 576 | response.SetVersion(request.GetVersion()) |
| 577 | response.SetXid(request.GetXid()) |
| 578 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 579 | return response, nil |
| 580 | } |
| 581 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 582 | func (ofc *OFConnection) handleQueueStatsRequest(request *ofp.QueueStatsRequest) (*ofp.QueueStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 583 | response := ofp.NewQueueStatsReply() |
| 584 | response.SetVersion(request.GetVersion()) |
| 585 | response.SetXid(request.GetXid()) |
| 586 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 587 | //TODO wire this to voltha core when it implements |
| 588 | return response, nil |
| 589 | } |
| 590 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 591 | func (ofc *OFConnection) handlePortStatsRequest(ctx context.Context, request *ofp.PortStatsRequest) ([]*ofp.PortStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 592 | volthaClient := ofc.VolthaClient.Get() |
| 593 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 594 | return nil, NoVolthaConnectionError |
| 595 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 596 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 597 | reply, err := volthaClient.ListLogicalDevicePorts(log.WithSpanFromContext(context.Background(), ctx), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 598 | &common.ID{Id: ofc.DeviceID}) |
| 599 | if err != nil { |
| 600 | return nil, err |
| 601 | } |
| 602 | var entries []*ofp.PortStatsEntry |
| 603 | if request.GetPortNo() == 0xffffffff { //all ports |
| 604 | for _, port := range reply.GetItems() { |
| 605 | entries = append(entries, parsePortStats(port)) |
| 606 | } |
| 607 | } else { //find right port that is requested |
| 608 | for _, port := range reply.GetItems() { |
| 609 | if port.GetOfpPortStats().GetPortNo() == uint32(request.GetPortNo()) { |
| 610 | entries = append(entries, parsePortStats(port)) |
| 611 | } |
| 612 | } |
| 613 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 614 | |
| 615 | var responses []*ofp.PortStatsReply |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 616 | |
Matteo Scandolo | 256266d | 2020-06-01 13:44:07 -0700 | [diff] [blame] | 617 | chunkSize := ofc.portsChunkSize |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 618 | total := len(entries) / chunkSize |
| 619 | n := 0 |
| 620 | for n <= total { |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 621 | response := ofp.NewPortStatsReply() |
| 622 | response.SetXid(request.GetXid()) |
| 623 | response.SetVersion(request.GetVersion()) |
| 624 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 625 | |
| 626 | chunk := entries[n*chunkSize : min((n*chunkSize)+chunkSize, len(entries))] |
| 627 | |
| 628 | if len(chunk) == 0 { |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 629 | // handle the case of no ports |
| 630 | if len(entries) == 0 { |
| 631 | logger.Debugw(ctx, "no-ports-present", log.Fields{"logical-device-id": ofc.DeviceID}) |
| 632 | response.SetEntries(chunk) |
| 633 | responses = append(responses, response) |
| 634 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 635 | break |
| 636 | } |
| 637 | |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 638 | if total != n { |
| 639 | response.SetFlags(ofp.StatsReplyFlags(ofp.OFPSFReplyMore)) |
| 640 | } |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 641 | response.SetEntries(chunk) |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 642 | responses = append(responses, response) |
| 643 | n++ |
| 644 | } |
| 645 | return responses, nil |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 648 | func (ofc *OFConnection) handlePortDescStatsRequest(ctx context.Context, request *ofp.PortDescStatsRequest) ([]*ofp.PortDescStatsReply, error) { |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 649 | volthaClient := ofc.VolthaClient.Get() |
| 650 | if volthaClient == nil { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 651 | return nil, NoVolthaConnectionError |
| 652 | } |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 653 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 654 | ports, err := volthaClient.ListLogicalDevicePorts(log.WithSpanFromContext(context.Background(), ctx), &common.ID{Id: ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 655 | if err != nil { |
| 656 | return nil, err |
| 657 | } |
| 658 | var entries []*ofp.PortDesc |
Kent Hagerman | 9b0ac0a | 2020-06-08 11:48:16 -0400 | [diff] [blame] | 659 | for _, port := range ports.Items { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 660 | ofpPort := port.GetOfpPort() |
| 661 | var entry ofp.PortDesc |
| 662 | entry.SetPortNo(ofp.Port(ofpPort.GetPortNo())) |
| 663 | |
| 664 | var octets []byte |
| 665 | for _, val := range ofpPort.GetHwAddr() { |
| 666 | octets = append(octets, byte(val)) |
| 667 | } |
| 668 | hwAddr := net.HardwareAddr(octets) |
| 669 | entry.SetHwAddr(hwAddr) |
| 670 | entry.SetName(PadString(ofpPort.GetName(), 16)) |
| 671 | entry.SetConfig(ofp.PortConfig(ofpPort.GetConfig())) |
| 672 | entry.SetState(ofp.PortState(ofpPort.GetState())) |
| 673 | entry.SetCurr(ofp.PortFeatures(ofpPort.GetCurr())) |
| 674 | entry.SetAdvertised(ofp.PortFeatures(ofpPort.GetAdvertised())) |
| 675 | entry.SetSupported(ofp.PortFeatures(ofpPort.GetSupported())) |
| 676 | entry.SetPeer(ofp.PortFeatures(ofpPort.GetPeer())) |
| 677 | entry.SetCurrSpeed(ofpPort.GetCurrSpeed()) |
| 678 | entry.SetMaxSpeed(ofpPort.GetMaxSpeed()) |
| 679 | |
| 680 | entries = append(entries, &entry) |
| 681 | } |
| 682 | |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 683 | var responses []*ofp.PortDescStatsReply |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 684 | |
Matteo Scandolo | 256266d | 2020-06-01 13:44:07 -0700 | [diff] [blame] | 685 | chunkSize := ofc.portsDescChunkSize |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 686 | total := len(entries) / chunkSize |
| 687 | n := 0 |
| 688 | for n <= total { |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 689 | response := ofp.NewPortDescStatsReply() |
| 690 | response.SetVersion(request.GetVersion()) |
| 691 | response.SetXid(request.GetXid()) |
| 692 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
khenaidoo | 47f99de | 2021-05-20 13:22:49 -0400 | [diff] [blame] | 693 | |
| 694 | chunk := entries[n*chunkSize : min((n*chunkSize)+chunkSize, len(entries))] |
| 695 | |
| 696 | if len(chunk) == 0 { |
| 697 | if len(entries) == 0 { |
| 698 | logger.Debugw(ctx, "no-ports-desc-present", log.Fields{"logical-device-id": ofc.DeviceID}) |
| 699 | response.SetEntries(chunk) |
| 700 | responses = append(responses, response) |
| 701 | } |
| 702 | break |
| 703 | } |
| 704 | |
Matteo Scandolo | e23b758 | 2020-05-21 13:50:02 -0700 | [diff] [blame] | 705 | if total != n { |
| 706 | response.SetFlags(ofp.StatsReplyFlags(ofp.OFPSFReplyMore)) |
| 707 | } |
| 708 | response.SetEntries(chunk) |
| 709 | responses = append(responses, response) |
| 710 | n++ |
| 711 | } |
| 712 | return responses, nil |
| 713 | |
| 714 | } |
| 715 | |
| 716 | // Interestingly enough there is no min function fot two integers |
| 717 | func min(a, b int) int { |
| 718 | if a < b { |
| 719 | return a |
| 720 | } |
| 721 | return b |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 724 | func (ofc *OFConnection) handleMeterFeatureStatsRequest(request *ofp.MeterFeaturesStatsRequest) (*ofp.MeterFeaturesStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 725 | response := ofp.NewMeterFeaturesStatsReply() |
| 726 | response.SetXid(request.GetXid()) |
| 727 | response.SetVersion(request.GetVersion()) |
| 728 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 729 | meterFeatures := ofp.NewMeterFeatures() |
| 730 | meterFeatures.Capabilities = ofp.OFPMFKbps |
| 731 | meterFeatures.BandTypes = ofp.OFPMBTDrop |
| 732 | meterFeatures.MaxMeter = 0xffffffff |
| 733 | meterFeatures.MaxBands = 0xff |
| 734 | meterFeatures.MaxColor = 0xff |
| 735 | response.Features = *meterFeatures |
| 736 | return response, nil |
| 737 | } |
| 738 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 739 | func (ofc *OFConnection) handleExperimenterStatsRequest(request *ofp.ExperimenterStatsRequest) (*ofp.ExperimenterStatsReply, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 740 | response := ofp.NewExperimenterStatsReply(request.GetExperimenter()) |
| 741 | response.SetVersion(request.GetVersion()) |
| 742 | response.SetXid(request.GetXid()) |
| 743 | response.SetFlags(ofp.StatsReplyFlags(request.GetFlags())) |
| 744 | //TODO wire this to voltha core when it implements |
| 745 | return response, nil |
| 746 | } |