Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 1 | /* |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 2 | Copyright 2020 the original author or authors. |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 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 | package openflow |
| 17 | |
| 18 | import ( |
Andrea Campanella | 07e9494 | 2020-07-20 17:35:46 +0200 | [diff] [blame] | 19 | "encoding/binary" |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 20 | "encoding/json" |
Andrea Campanella | 07e9494 | 2020-07-20 17:35:46 +0200 | [diff] [blame] | 21 | "unsafe" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 22 | |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 23 | ofp "github.com/opencord/goloxi/of13" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 25 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 26 | "golang.org/x/net/context" |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 29 | func (ofc *OFConnection) handleMeterModRequest(ctx context.Context, request *ofp.MeterMod) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 30 | span, ctx := log.CreateChildSpan(ctx, "openflow-meter-modification") |
| 31 | defer span.Finish() |
| 32 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 33 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 34 | js, _ := json.Marshal(request) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 35 | logger.Debugw(ctx, "handleMeterModRequest called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 36 | log.Fields{ |
| 37 | "device-id": ofc.DeviceID, |
| 38 | "request": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 39 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 40 | |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 41 | volthaClient := ofc.VolthaClient.Get() |
| 42 | if volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 43 | logger.Errorw(ctx, "no-voltha-connection", |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 44 | log.Fields{"device-id": ofc.DeviceID}) |
| 45 | return |
| 46 | } |
| 47 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 48 | meterModUpdate := openflow_13.MeterModUpdate{Id: ofc.DeviceID} |
| 49 | meterMod := openflow_13.OfpMeterMod{ |
| 50 | MeterId: request.MeterId, |
| 51 | Flags: uint32(request.Flags), |
| 52 | Command: openflow_13.OfpMeterModCommand(request.Command), |
| 53 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 54 | var bands []*openflow_13.OfpMeterBandHeader |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 55 | for _, ofpBand := range request.GetMeters() { |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 56 | var band openflow_13.OfpMeterBandHeader |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 57 | switch ofpBand.GetType() { |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 58 | case ofp.OFPMBTDrop: |
| 59 | ofpDrop := ofpBand.(ofp.IMeterBandDrop) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 60 | band.Type = openflow_13.OfpMeterBandType_OFPMBT_DROP |
| 61 | band.Rate = ofpDrop.GetRate() |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 62 | band.BurstSize = ofpDrop.GetBurstSize() |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 63 | case ofp.OFPMBTDSCPRemark: |
| 64 | ofpDscpRemark := ofpBand.(ofp.IMeterBandDscpRemark) |
| 65 | var dscpRemark openflow_13.OfpMeterBandDscpRemark |
| 66 | band.Type = openflow_13.OfpMeterBandType_OFPMBT_DSCP_REMARK |
| 67 | band.BurstSize = ofpDscpRemark.GetBurstSize() |
| 68 | band.Rate = ofpDscpRemark.GetRate() |
| 69 | dscpRemark.PrecLevel = uint32(ofpDscpRemark.GetPrecLevel()) |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 70 | /* |
| 71 | var meterBandHeaderDscp openflow_13.OfpMeterBandHeader_DscpRemark |
| 72 | meterBandHeaderDscp.DscpRemark = &dscpRemark |
| 73 | band.Data = &meterBandHeaderDscp |
| 74 | |
| 75 | */ |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 76 | case ofp.OFPMBTExperimenter: |
| 77 | ofpExperimenter := ofpBand.(ofp.IMeterBandExperimenter) |
| 78 | var experimenter openflow_13.OfpMeterBandExperimenter |
| 79 | experimenter.Experimenter = ofpExperimenter.GetExperimenter() |
| 80 | band.Type = openflow_13.OfpMeterBandType_OFPMBT_EXPERIMENTER |
| 81 | band.BurstSize = ofpExperimenter.GetBurstSize() |
| 82 | band.Rate = ofpExperimenter.GetRate() |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 83 | /* |
| 84 | var meterBandHeaderExperimenter openflow_13.OfpMeterBandHeader_Experimenter |
| 85 | meterBandHeaderExperimenter.Experimenter = &experimenter |
| 86 | band.Data = &meterBandHeaderExperimenter |
| 87 | |
| 88 | */ |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 89 | } |
| 90 | bands = append(bands, &band) |
| 91 | } |
| 92 | meterMod.Bands = bands |
| 93 | meterModUpdate.MeterMod = &meterMod |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 94 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 95 | meterModJS, _ := json.Marshal(meterModUpdate) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 96 | logger.Debugw(ctx, "handleMeterModUpdate sending request", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 97 | log.Fields{ |
| 98 | "device-id": ofc.DeviceID, |
| 99 | "meter-mod-request": meterModJS}) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 100 | } |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 101 | if _, err := volthaClient.UpdateLogicalDeviceMeterTable(log.WithSpanFromContext(context.Background(), ctx), &meterModUpdate); err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 102 | logger.Errorw(ctx, "Error calling UpdateLogicalDeviceMeterTable", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 103 | log.Fields{ |
| 104 | "device-id": ofc.DeviceID, |
| 105 | "error": err}) |
Andrea Campanella | 07e9494 | 2020-07-20 17:35:46 +0200 | [diff] [blame] | 106 | message := ofp.NewMeterModFailedErrorMsg() |
| 107 | message.SetXid(request.Xid) |
| 108 | //OF 1.3 |
| 109 | message.SetVersion(4) |
| 110 | //FIXME Hardcoding to Invalid Meter for now. |
| 111 | message.SetCode(ofp.OFPMMFCInvalidMeter) |
| 112 | meterByteId := make([]byte, 4) |
| 113 | binary.BigEndian.PutUint32(meterByteId, request.MeterId) |
| 114 | message.SetData(meterByteId) |
| 115 | message.Length = uint16(unsafe.Sizeof(*message)) |
| 116 | if err := ofc.SendMessage(ctx, message); err != nil { |
| 117 | logger.Errorw(ctx, "Error reporting failure of MeterMod to controller", |
| 118 | log.Fields{ |
| 119 | "device-id": ofc.DeviceID, |
| 120 | "error": err}) |
| 121 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 122 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 123 | } |