khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 360605d | 2019-11-05 18:29:17 -0800 | [diff] [blame] | 21 | "encoding/hex" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 22 | "errors" |
| 23 | "fmt" |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 24 | "reflect" |
| 25 | "sync" |
| 26 | "time" |
| 27 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | "github.com/gogo/protobuf/proto" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/db/model" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 30 | fd "github.com/opencord/voltha-go/rw_core/flowdecomposition" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-go/rw_core/route" |
Scott Baker | b671a86 | 2019-10-24 10:53:40 -0700 | [diff] [blame] | 32 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 33 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 34 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 35 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 36 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 37 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | "google.golang.org/grpc/codes" |
| 39 | "google.golang.org/grpc/status" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | const ( |
| 43 | maxOrderedLogicalDeviceRequestQueueSize = 1000 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 44 | ) |
| 45 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 46 | // LogicalDeviceAgent represent attributes of logical device agent |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 47 | type LogicalDeviceAgent struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 48 | logicalDeviceID string |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 49 | serialNumber string |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 50 | rootDeviceID string |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 51 | deviceMgr *DeviceManager |
| 52 | ldeviceMgr *LogicalDeviceManager |
| 53 | clusterDataProxy *model.Proxy |
| 54 | exitChannel chan int |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 55 | deviceRoutes *route.DeviceRoutes |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 56 | flowProxy *model.Proxy |
| 57 | groupProxy *model.Proxy |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 58 | meterProxy *model.Proxy |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 59 | ldProxy *model.Proxy |
| 60 | portProxies map[string]*model.Proxy |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 61 | lockDeviceRoutes sync.RWMutex |
khenaidoo | 3306c99 | 2019-05-24 16:57:35 -0400 | [diff] [blame] | 62 | logicalPortsNo map[uint32]bool //value is true for NNI port |
| 63 | lockLogicalPortsNo sync.RWMutex |
| 64 | flowDecomposer *fd.FlowDecomposer |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 65 | defaultTimeout time.Duration |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 66 | logicalDevice *voltha.LogicalDevice |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 67 | requestQueue *coreutils.RequestQueue |
| 68 | startOnce sync.Once |
| 69 | stopOnce sync.Once |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 70 | } |
| 71 | |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 72 | func newLogicalDeviceAgent(id string, sn string, deviceID string, ldeviceMgr *LogicalDeviceManager, |
| 73 | deviceMgr *DeviceManager, cdProxy *model.Proxy, timeout time.Duration) *LogicalDeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 74 | var agent LogicalDeviceAgent |
| 75 | agent.exitChannel = make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 76 | agent.logicalDeviceID = id |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 77 | agent.serialNumber = sn |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 78 | agent.rootDeviceID = deviceID |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 79 | agent.deviceMgr = deviceMgr |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 80 | agent.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 81 | agent.ldeviceMgr = ldeviceMgr |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 82 | agent.flowDecomposer = fd.NewFlowDecomposer(agent.deviceMgr) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 83 | agent.portProxies = make(map[string]*model.Proxy) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 84 | agent.logicalPortsNo = make(map[uint32]bool) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 85 | agent.defaultTimeout = timeout |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 86 | agent.requestQueue = coreutils.NewRequestQueue(agent.serialNumber, maxOrderedLogicalDeviceRequestQueueSize) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 87 | return &agent |
| 88 | } |
| 89 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 90 | // start creates the logical device and add it to the data model |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 91 | func (agent *LogicalDeviceAgent) start(ctx context.Context, loadFromDB bool) error { |
| 92 | needToStart := false |
| 93 | if agent.startOnce.Do(func() { needToStart = true }); !needToStart { |
| 94 | return nil |
| 95 | } |
| 96 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 97 | logger.Infow("starting-logical_device-agent", log.Fields{"logical-device-id": agent.logicalDeviceID, "load-from-db": loadFromDB}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 98 | |
| 99 | var startSucceeded bool |
| 100 | defer func() { |
| 101 | if !startSucceeded { |
| 102 | if err := agent.stop(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 103 | logger.Errorw("failed-to-cleanup-after-unsuccessful-start", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | }() |
| 107 | |
| 108 | // Launch the request queue - it will launch a go routine |
| 109 | agent.requestQueue.Start() |
| 110 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 111 | var ld *voltha.LogicalDevice |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 112 | if !loadFromDB { |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 113 | //Build the logical device based on information retrieved from the device adapter |
| 114 | var switchCap *ic.SwitchCapability |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 115 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 116 | if switchCap, err = agent.deviceMgr.getSwitchCapability(ctx, agent.rootDeviceID); err != nil { |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 117 | return err |
| 118 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 119 | ld = &voltha.LogicalDevice{Id: agent.logicalDeviceID, RootDeviceId: agent.rootDeviceID} |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 120 | |
| 121 | // Create the datapath ID (uint64) using the logical device ID (based on the MAC Address) |
| 122 | var datapathID uint64 |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 123 | if datapathID, err = CreateDataPathID(agent.serialNumber); err != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 124 | return err |
| 125 | } |
| 126 | ld.DatapathId = datapathID |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 127 | ld.Desc = (proto.Clone(switchCap.Desc)).(*ofp.OfpDesc) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 128 | logger.Debugw("Switch-capability", log.Fields{"Desc": ld.Desc, "fromAd": switchCap.Desc}) |
khenaidoo | 7e3d8f1 | 2019-08-02 16:06:30 -0400 | [diff] [blame] | 129 | ld.SwitchFeatures = (proto.Clone(switchCap.SwitchFeatures)).(*ofp.OfpSwitchFeatures) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 130 | ld.Flows = &ofp.Flows{Items: nil} |
| 131 | ld.FlowGroups = &ofp.FlowGroups{Items: nil} |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 132 | ld.Ports = []*voltha.LogicalPort{} |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 133 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 134 | // Save the logical device |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 135 | added, err := agent.clusterDataProxy.AddWithID(ctx, "/logical_devices", ld.Id, ld, "") |
| 136 | if err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 137 | return err |
| 138 | } |
| 139 | if added == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 140 | logger.Errorw("failed-to-add-logical-device", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 141 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 142 | logger.Debugw("logicaldevice-created", log.Fields{"logical-device-id": agent.logicalDeviceID, "root-id": ld.RootDeviceId}) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 143 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 144 | |
| 145 | agent.logicalDevice = proto.Clone(ld).(*voltha.LogicalDevice) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 146 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 147 | // Setup the logicalports - internal processing, no need to propagate the client context |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 148 | go func() { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 149 | err := agent.setupLogicalPorts(context.Background()) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 150 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 151 | logger.Errorw("unable-to-setup-logical-ports", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 152 | } |
| 153 | }() |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 154 | } else { |
| 155 | // load from dB - the logical may not exist at this time. On error, just return and the calling function |
| 156 | // will destroy this agent. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 157 | logicalDevice, err := agent.clusterDataProxy.Get(ctx, "/logical_devices/"+agent.logicalDeviceID, 0, true, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 158 | if err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 159 | return err |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 160 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 161 | ld, ok := logicalDevice.(*voltha.LogicalDevice) |
| 162 | if !ok { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 163 | return status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 164 | } |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 165 | // Update the root device Id |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 166 | agent.rootDeviceID = ld.RootDeviceId |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 167 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 168 | // Update the last data |
| 169 | agent.logicalDevice = proto.Clone(ld).(*voltha.LogicalDevice) |
| 170 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 171 | // Setup the local list of logical ports |
| 172 | agent.addLogicalPortsToMap(ld.Ports) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 173 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 174 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 175 | var err error |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 176 | agent.flowProxy, err = agent.clusterDataProxy.CreateProxy( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 177 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 178 | fmt.Sprintf("/logical_devices/%s/flows", agent.logicalDeviceID), |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 179 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 180 | if err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 181 | return err |
| 182 | } |
| 183 | agent.meterProxy, err = agent.clusterDataProxy.CreateProxy( |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 184 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 185 | fmt.Sprintf("/logical_devices/%s/meters", agent.logicalDeviceID), |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 186 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 187 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 188 | logger.Errorw("failed-to-create-meter-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 189 | return err |
| 190 | } |
| 191 | agent.groupProxy, err = agent.clusterDataProxy.CreateProxy( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 192 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 193 | fmt.Sprintf("/logical_devices/%s/flow_groups", agent.logicalDeviceID), |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 194 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 195 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 196 | logger.Errorw("failed-to-create-group-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 197 | return err |
| 198 | } |
| 199 | agent.ldProxy, err = agent.clusterDataProxy.CreateProxy( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 200 | ctx, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 201 | fmt.Sprintf("/logical_devices/%s", agent.logicalDeviceID), |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 202 | false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 203 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 204 | logger.Errorw("failed-to-create-logical-device-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 205 | return err |
| 206 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 207 | // TODO: Use a port proxy once the POST_ADD is fixed |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 208 | if agent.ldProxy != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 209 | agent.ldProxy.RegisterCallback(model.PostUpdate, agent.portUpdated) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 210 | } else { |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 211 | return status.Error(codes.Internal, "logical-device-proxy-null") |
| 212 | } |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 213 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 214 | // Setup the device routes. Building routes may fail if the pre-conditions are not satisfied (e.g. no PON ports present) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 215 | if loadFromDB { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 216 | go func() { |
| 217 | if err := agent.buildRoutes(context.Background()); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 218 | logger.Warn("routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 219 | } |
| 220 | }() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 221 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 222 | startSucceeded = true |
| 223 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 224 | return nil |
| 225 | } |
| 226 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 227 | // stop stops the logical device agent. This removes the logical device from the data model. |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 228 | func (agent *LogicalDeviceAgent) stop(ctx context.Context) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 229 | var returnErr error |
| 230 | agent.stopOnce.Do(func() { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 231 | logger.Info("stopping-logical_device-agent") |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 232 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 233 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 234 | // This should never happen - an error is returned only if the agent is stopped and an agent is only stopped once. |
| 235 | returnErr = err |
| 236 | return |
| 237 | } |
| 238 | defer agent.requestQueue.RequestComplete() |
| 239 | |
| 240 | //Remove the logical device from the model |
| 241 | if removed, err := agent.clusterDataProxy.Remove(ctx, "/logical_devices/"+agent.logicalDeviceID, ""); err != nil { |
| 242 | returnErr = err |
| 243 | } else if removed == nil { |
| 244 | returnErr = status.Errorf(codes.Aborted, "failed-to-remove-logical-ldevice-%s", agent.logicalDeviceID) |
| 245 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 246 | logger.Debugw("logicaldevice-removed", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Stop the request queue and request complete indication |
| 250 | agent.requestQueue.Stop() |
| 251 | |
| 252 | close(agent.exitChannel) |
| 253 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 254 | logger.Info("logical_device-agent-stopped") |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 255 | }) |
| 256 | return returnErr |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 257 | } |
| 258 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 259 | // GetLogicalDevice returns the latest logical device data |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 260 | func (agent *LogicalDeviceAgent) GetLogicalDevice(ctx context.Context) (*voltha.LogicalDevice, error) { |
| 261 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 262 | return nil, err |
| 263 | } |
| 264 | defer agent.requestQueue.RequestComplete() |
| 265 | return proto.Clone(agent.logicalDevice).(*voltha.LogicalDevice), nil |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 266 | } |
| 267 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 268 | // ListLogicalDeviceFlows returns logical device flows |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 269 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows(ctx context.Context) (*ofp.Flows, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 270 | logger.Debug("ListLogicalDeviceFlows") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 271 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 272 | logicalDevice, err := agent.GetLogicalDevice(ctx) |
| 273 | if err != nil { |
| 274 | return nil, err |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 275 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 276 | if logicalDevice.Flows == nil { |
| 277 | return &ofp.Flows{}, nil |
| 278 | } |
| 279 | return (proto.Clone(logicalDevice.Flows)).(*ofp.Flows), nil |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 280 | } |
| 281 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 282 | // ListLogicalDeviceMeters returns logical device meters |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 283 | func (agent *LogicalDeviceAgent) ListLogicalDeviceMeters(ctx context.Context) (*ofp.Meters, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 284 | logger.Debug("ListLogicalDeviceMeters") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 285 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 286 | logicalDevice, err := agent.GetLogicalDevice(ctx) |
| 287 | if err != nil { |
| 288 | return nil, err |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 289 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 290 | if logicalDevice.Meters == nil { |
| 291 | return &ofp.Meters{}, nil |
| 292 | } |
| 293 | return (proto.Clone(logicalDevice.Meters)).(*ofp.Meters), nil |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 294 | } |
| 295 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 296 | // ListLogicalDeviceFlowGroups returns logical device flow groups |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 297 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups(ctx context.Context) (*ofp.FlowGroups, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 298 | logger.Debug("ListLogicalDeviceFlowGroups") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 299 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 300 | logicalDevice, err := agent.GetLogicalDevice(ctx) |
| 301 | if err != nil { |
| 302 | return nil, err |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 303 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 304 | if logicalDevice.FlowGroups == nil { |
| 305 | return &ofp.FlowGroups{}, nil |
| 306 | } |
| 307 | return (proto.Clone(logicalDevice.FlowGroups)).(*ofp.FlowGroups), nil |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 308 | } |
| 309 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 310 | // ListLogicalDevicePorts returns logical device ports |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 311 | func (agent *LogicalDeviceAgent) ListLogicalDevicePorts(ctx context.Context) (*voltha.LogicalPorts, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 312 | logger.Debug("ListLogicalDevicePorts") |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 313 | logicalDevice, err := agent.GetLogicalDevice(ctx) |
| 314 | if err != nil { |
| 315 | return nil, err |
| 316 | } |
| 317 | if logicalDevice == nil { |
| 318 | return &voltha.LogicalPorts{}, nil |
| 319 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 320 | lPorts := make([]*voltha.LogicalPort, 0) |
| 321 | lPorts = append(lPorts, logicalDevice.Ports...) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 322 | return &voltha.LogicalPorts{Items: lPorts}, nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 323 | } |
| 324 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 325 | //updateLogicalDeviceFlowsWithoutLock updates the logical device with the latest flows in the model. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 326 | func (agent *LogicalDeviceAgent) updateLogicalDeviceFlowsWithoutLock(ctx context.Context, flows *ofp.Flows) error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 327 | ld := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 328 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 329 | logger.Debugw("logical-device-before", log.Fields{"lports": len(ld.Ports)}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 330 | ld.Flows = flows |
| 331 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 332 | if err := agent.updateLogicalDeviceWithoutLock(ctx, ld); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 333 | logger.Errorw("error-updating-logical-device-with-flows", log.Fields{"error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 334 | return err |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 335 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 336 | return nil |
| 337 | } |
| 338 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 339 | //updateLogicalDeviceMetersWithoutLock updates the logical device with the meters info |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 340 | func (agent *LogicalDeviceAgent) updateLogicalDeviceMetersWithoutLock(ctx context.Context, meters *ofp.Meters) error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 341 | ld := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 342 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 343 | logger.Debugw("logical-device-before", log.Fields{"lports": len(ld.Ports)}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 344 | ld.Meters = meters |
| 345 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 346 | if err := agent.updateLogicalDeviceWithoutLock(ctx, ld); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 347 | logger.Errorw("error-updating-logical-device-with-meters", log.Fields{"error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 348 | return err |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 349 | } |
| 350 | return nil |
| 351 | } |
| 352 | |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 353 | //updateLogicalDeviceFlowGroupsWithoutLock updates the logical device with the flow groups |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 354 | func (agent *LogicalDeviceAgent) updateLogicalDeviceFlowGroupsWithoutLock(ctx context.Context, flowGroups *ofp.FlowGroups) error { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 355 | ld := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 356 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 357 | logger.Debugw("logical-device-before", log.Fields{"lports": len(ld.Ports)}) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 358 | ld.FlowGroups = flowGroups |
| 359 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 360 | if err := agent.updateLogicalDeviceWithoutLock(ctx, ld); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 361 | logger.Errorw("error-updating-logical-device-with-flowgroups", log.Fields{"error": err}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 362 | return err |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 363 | } |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 364 | return nil |
| 365 | } |
| 366 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 367 | // getLogicalDeviceWithoutLock returns a cloned logical device to a function that already holds the agent lock. |
| 368 | func (agent *LogicalDeviceAgent) getLogicalDeviceWithoutLock() *voltha.LogicalDevice { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 369 | logger.Debug("getLogicalDeviceWithoutLock") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 370 | return proto.Clone(agent.logicalDevice).(*voltha.LogicalDevice) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 371 | } |
| 372 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 373 | func (agent *LogicalDeviceAgent) updateLogicalPort(ctx context.Context, device *voltha.Device, port *voltha.Port) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 374 | logger.Debugw("updateLogicalPort", log.Fields{"deviceId": device.Id, "port": port}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 375 | var err error |
| 376 | if port.Type == voltha.Port_ETHERNET_NNI { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 377 | if _, err = agent.addNNILogicalPort(ctx, device, port); err != nil { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 378 | return err |
| 379 | } |
| 380 | agent.addLogicalPortToMap(port.PortNo, true) |
| 381 | } else if port.Type == voltha.Port_ETHERNET_UNI { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 382 | if _, err = agent.addUNILogicalPort(ctx, device, port); err != nil { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 383 | return err |
| 384 | } |
| 385 | agent.addLogicalPortToMap(port.PortNo, false) |
| 386 | } else { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 387 | // Update the device routes to ensure all routes on the logical device have been calculated |
| 388 | if err = agent.buildRoutes(ctx); err != nil { |
| 389 | // Not an error - temporary state |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 390 | logger.Warnw("failed-to-update-routes", log.Fields{"device-id": device.Id, "port": port, "error": err}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | return nil |
| 394 | } |
| 395 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 396 | // setupLogicalPorts is invoked once the logical device has been created and is ready to get ports |
| 397 | // added to it. While the logical device was being created we could have received requests to add |
| 398 | // NNI and UNI ports which were discarded. Now is the time to add them if needed |
| 399 | func (agent *LogicalDeviceAgent) setupLogicalPorts(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 400 | logger.Infow("setupLogicalPorts", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 401 | // First add any NNI ports which could have been missing |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 402 | if err := agent.setupNNILogicalPorts(ctx, agent.rootDeviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 403 | logger.Errorw("error-setting-up-NNI-ports", log.Fields{"error": err, "deviceId": agent.rootDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 404 | return err |
| 405 | } |
| 406 | |
| 407 | // Now, set up the UNI ports if needed. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 408 | children, err := agent.deviceMgr.getAllChildDevices(ctx, agent.rootDeviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 409 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 410 | logger.Errorw("error-getting-child-devices", log.Fields{"error": err, "deviceId": agent.rootDeviceID}) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 411 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 412 | } |
| 413 | responses := make([]coreutils.Response, 0) |
| 414 | for _, child := range children.Items { |
| 415 | response := coreutils.NewResponse() |
| 416 | responses = append(responses, response) |
| 417 | go func(child *voltha.Device) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 418 | if err = agent.setupUNILogicalPorts(context.Background(), child); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 419 | logger.Error("setting-up-UNI-ports-failed", log.Fields{"deviceID": child.Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 420 | response.Error(status.Errorf(codes.Internal, "UNI-ports-setup-failed: %s", child.Id)) |
| 421 | } |
| 422 | response.Done() |
| 423 | }(child) |
| 424 | } |
| 425 | // Wait for completion |
| 426 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, responses...); res != nil { |
| 427 | return status.Errorf(codes.Aborted, "errors-%s", res) |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 428 | } |
| 429 | return nil |
| 430 | } |
| 431 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 432 | // setupNNILogicalPorts creates an NNI port on the logical device that represents an NNI interface on a root device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 433 | func (agent *LogicalDeviceAgent) setupNNILogicalPorts(ctx context.Context, deviceID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 434 | logger.Infow("setupNNILogicalPorts-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 435 | // Build the logical device based on information retrieved from the device adapter |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 436 | var err error |
| 437 | |
| 438 | var device *voltha.Device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 439 | if device, err = agent.deviceMgr.GetDevice(ctx, deviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 440 | logger.Errorw("error-retrieving-device", log.Fields{"error": err, "deviceId": deviceID}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 441 | return err |
| 442 | } |
| 443 | |
| 444 | //Get UNI port number |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 445 | for _, port := range device.Ports { |
| 446 | if port.Type == voltha.Port_ETHERNET_NNI { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 447 | if _, err = agent.addNNILogicalPort(ctx, device, port); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 448 | logger.Errorw("error-adding-UNI-port", log.Fields{"error": err}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 449 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 450 | agent.addLogicalPortToMap(port.PortNo, true) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 451 | } |
| 452 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 453 | return err |
| 454 | } |
| 455 | |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 456 | // updatePortState updates the port state of the device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 457 | func (agent *LogicalDeviceAgent) updatePortState(ctx context.Context, deviceID string, portNo uint32, operStatus voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 458 | logger.Infow("updatePortState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "portNo": portNo, "state": operStatus}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 459 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 460 | return err |
| 461 | } |
| 462 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 463 | // Get the latest logical device info |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 464 | cloned := agent.getLogicalDeviceWithoutLock() |
| 465 | for idx, lPort := range cloned.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 466 | if lPort.DeviceId == deviceID && lPort.DevicePortNo == portNo { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 467 | if operStatus == voltha.OperStatus_ACTIVE { |
| 468 | cloned.Ports[idx].OfpPort.Config = cloned.Ports[idx].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 469 | cloned.Ports[idx].OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
| 470 | } else { |
| 471 | cloned.Ports[idx].OfpPort.Config = cloned.Ports[idx].OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 472 | cloned.Ports[idx].OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
| 473 | } |
| 474 | // Update the logical device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 475 | if err := agent.updateLogicalDeviceWithoutLock(ctx, cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 476 | logger.Errorw("error-updating-logical-device", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 477 | return err |
| 478 | } |
| 479 | return nil |
| 480 | } |
| 481 | } |
| 482 | return status.Errorf(codes.NotFound, "port-%d-not-exist", portNo) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 483 | } |
| 484 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 485 | // updatePortsState updates the ports state related to the device |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 486 | func (agent *LogicalDeviceAgent) updatePortsState(ctx context.Context, device *voltha.Device, state voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 487 | logger.Infow("updatePortsState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 488 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 489 | return err |
| 490 | } |
| 491 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 492 | // Get the latest logical device info |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 493 | cloned := agent.getLogicalDeviceWithoutLock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 494 | for _, lport := range cloned.Ports { |
| 495 | if lport.DeviceId == device.Id { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 496 | if state == voltha.OperStatus_ACTIVE { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 497 | lport.OfpPort.Config = lport.OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 498 | lport.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 499 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 500 | lport.OfpPort.Config = lport.OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 501 | lport.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 502 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 503 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 504 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 505 | } |
| 506 | // Updating the logical device will trigger the poprt change events to be populated to the controller |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 507 | if err := agent.updateLogicalDeviceWithoutLock(ctx, cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 508 | logger.Warnw("logical-device-update-failed", log.Fields{"ldeviceId": agent.logicalDeviceID, "error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 509 | return err |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 510 | } |
| 511 | return nil |
| 512 | } |
| 513 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 514 | // setupUNILogicalPorts creates a UNI port on the logical device that represents a child UNI interface |
| 515 | func (agent *LogicalDeviceAgent) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 516 | logger.Infow("setupUNILogicalPort", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 517 | // Build the logical device based on information retrieved from the device adapter |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 518 | var err error |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 519 | var added bool |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 520 | //Get UNI port number |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 521 | for _, port := range childDevice.Ports { |
| 522 | if port.Type == voltha.Port_ETHERNET_UNI { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 523 | if added, err = agent.addUNILogicalPort(ctx, childDevice, port); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 524 | logger.Errorw("error-adding-UNI-port", log.Fields{"error": err}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 525 | } |
khenaidoo | 59ef7be | 2019-06-21 12:40:28 -0400 | [diff] [blame] | 526 | if added { |
| 527 | agent.addLogicalPortToMap(port.PortNo, false) |
| 528 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 529 | } |
| 530 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 531 | return err |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 534 | // deleteAllLogicalPorts deletes all logical ports associated with this logical device |
| 535 | func (agent *LogicalDeviceAgent) deleteAllLogicalPorts(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 536 | logger.Infow("updatePortsState-start", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 537 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 538 | return err |
| 539 | } |
| 540 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 541 | // Get the latest logical device info |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 542 | ld := agent.getLogicalDeviceWithoutLock() |
| 543 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 544 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 545 | var updateLogicalPorts []*voltha.LogicalPort |
| 546 | // Update an empty ports slice to remove all the ports |
| 547 | cloned.Ports = updateLogicalPorts |
| 548 | |
| 549 | if err := agent.updateLogicalDeviceWithoutLock(ctx, cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 550 | logger.Warnw("logical-device-update-failed", log.Fields{"ldeviceId": agent.logicalDeviceID, "error": err}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 551 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 552 | } |
| 553 | return nil |
| 554 | } |
| 555 | |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 556 | // deleteAllUNILogicalPorts deletes all UNI logical ports associated with this parent device |
| 557 | func (agent *LogicalDeviceAgent) deleteAllUNILogicalPorts(ctx context.Context, parentDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 558 | logger.Debugw("delete-all-uni-logical-ports", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 559 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 560 | return err |
| 561 | } |
| 562 | defer agent.requestQueue.RequestComplete() |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 563 | // Get the latest logical device info |
| 564 | ld := agent.getLogicalDeviceWithoutLock() |
| 565 | |
| 566 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 567 | updateLogicalPorts := []*voltha.LogicalPort{} |
| 568 | for _, lport := range cloned.Ports { |
| 569 | // Save NNI ports only |
| 570 | if agent.isNNIPort(lport.DevicePortNo) { |
| 571 | updateLogicalPorts = append(updateLogicalPorts, lport) |
| 572 | } |
| 573 | } |
| 574 | if len(updateLogicalPorts) < len(cloned.Ports) { |
| 575 | cloned.Ports = updateLogicalPorts |
| 576 | // Updating the logical device will trigger the port change events to be populated to the controller |
| 577 | if err := agent.updateLogicalDeviceWithoutLock(ctx, cloned); err != nil { |
| 578 | return err |
| 579 | } |
| 580 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 581 | logger.Debugw("no-change-required", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 582 | } |
| 583 | return nil |
| 584 | } |
| 585 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 586 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 587 | func (agent *LogicalDeviceAgent) updateLogicalDeviceWithoutLock(ctx context.Context, logicalDevice *voltha.LogicalDevice) error { |
| 588 | updateCtx := context.WithValue(ctx, model.RequestTimestamp, time.Now().UnixNano()) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 589 | afterUpdate, err := agent.clusterDataProxy.Update(updateCtx, "/logical_devices/"+agent.logicalDeviceID, logicalDevice, false, "") |
| 590 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 591 | logger.Errorw("failed-to-update-logical-devices-to-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 592 | return err |
| 593 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 594 | if afterUpdate == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 595 | return status.Errorf(codes.Internal, "failed-updating-logical-device:%s", agent.logicalDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 596 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 597 | //agent.logicalDevice = (proto.Clone(logicalDevice)).(*voltha.LogicalDevice) |
| 598 | agent.logicalDevice = logicalDevice |
| 599 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 600 | return nil |
| 601 | } |
| 602 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 603 | //generateDeviceRoutesIfNeeded generates the device routes if the logical device has been updated since the last time |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 604 | //that device graph was generated. |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 605 | func (agent *LogicalDeviceAgent) generateDeviceRoutesIfNeeded(ctx context.Context) error { |
| 606 | agent.lockDeviceRoutes.Lock() |
| 607 | defer agent.lockDeviceRoutes.Unlock() |
| 608 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 609 | ld, err := agent.GetLogicalDevice(ctx) |
| 610 | if err != nil { |
| 611 | return err |
| 612 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 613 | |
| 614 | if agent.deviceRoutes != nil && agent.deviceRoutes.IsUpToDate(ld) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 615 | return nil |
| 616 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 617 | logger.Debug("Generation of device route required") |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 618 | if err := agent.buildRoutes(ctx); err != nil { |
| 619 | return err |
| 620 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 621 | return nil |
| 622 | } |
| 623 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 624 | //updateFlowTable updates the flow table of that logical device |
| 625 | func (agent *LogicalDeviceAgent) updateFlowTable(ctx context.Context, flow *ofp.OfpFlowMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 626 | logger.Debug("updateFlowTable") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 627 | if flow == nil { |
| 628 | return nil |
| 629 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 630 | if err := agent.generateDeviceRoutesIfNeeded(ctx); err != nil { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 631 | return err |
| 632 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 633 | switch flow.GetCommand() { |
| 634 | case ofp.OfpFlowModCommand_OFPFC_ADD: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 635 | return agent.flowAdd(ctx, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 636 | case ofp.OfpFlowModCommand_OFPFC_DELETE: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 637 | return agent.flowDelete(ctx, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 638 | case ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 639 | return agent.flowDeleteStrict(ctx, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 640 | case ofp.OfpFlowModCommand_OFPFC_MODIFY: |
| 641 | return agent.flowModify(flow) |
| 642 | case ofp.OfpFlowModCommand_OFPFC_MODIFY_STRICT: |
| 643 | return agent.flowModifyStrict(flow) |
| 644 | } |
| 645 | return status.Errorf(codes.Internal, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 646 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, flow.GetCommand()) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | //updateGroupTable updates the group table of that logical device |
| 650 | func (agent *LogicalDeviceAgent) updateGroupTable(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 651 | logger.Debug("updateGroupTable") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 652 | if groupMod == nil { |
| 653 | return nil |
| 654 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 655 | if err := agent.generateDeviceRoutesIfNeeded(ctx); err != nil { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 656 | return err |
| 657 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 658 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 659 | switch groupMod.GetCommand() { |
| 660 | case ofp.OfpGroupModCommand_OFPGC_ADD: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 661 | return agent.groupAdd(ctx, groupMod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 662 | case ofp.OfpGroupModCommand_OFPGC_DELETE: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 663 | return agent.groupDelete(ctx, groupMod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 664 | case ofp.OfpGroupModCommand_OFPGC_MODIFY: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 665 | return agent.groupModify(ctx, groupMod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 666 | } |
| 667 | return status.Errorf(codes.Internal, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 668 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, groupMod.GetCommand()) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 669 | } |
| 670 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 671 | // updateMeterTable updates the meter table of that logical device |
| 672 | func (agent *LogicalDeviceAgent) updateMeterTable(ctx context.Context, meterMod *ofp.OfpMeterMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 673 | logger.Debug("updateMeterTable") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 674 | if meterMod == nil { |
| 675 | return nil |
| 676 | } |
| 677 | switch meterMod.GetCommand() { |
| 678 | case ofp.OfpMeterModCommand_OFPMC_ADD: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 679 | return agent.meterAdd(ctx, meterMod) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 680 | case ofp.OfpMeterModCommand_OFPMC_DELETE: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 681 | return agent.meterDelete(ctx, meterMod) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 682 | case ofp.OfpMeterModCommand_OFPMC_MODIFY: |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 683 | return agent.meterModify(ctx, meterMod) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 684 | } |
| 685 | return status.Errorf(codes.Internal, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 686 | "unhandled-command: lDeviceId:%s, command:%s", agent.logicalDeviceID, meterMod.GetCommand()) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 687 | |
| 688 | } |
| 689 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 690 | func (agent *LogicalDeviceAgent) meterAdd(ctx context.Context, meterMod *ofp.OfpMeterMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 691 | logger.Debugw("meterAdd", log.Fields{"metermod": *meterMod}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 692 | if meterMod == nil { |
| 693 | return nil |
| 694 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 695 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 696 | return err |
| 697 | } |
| 698 | defer agent.requestQueue.RequestComplete() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 699 | logger.Debug("Acquired logical device lock") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 700 | lDevice := agent.getLogicalDeviceWithoutLock() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 701 | |
| 702 | var meters []*ofp.OfpMeterEntry |
| 703 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 704 | meters = lDevice.Meters.Items |
| 705 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 706 | logger.Debugw("Available meters", log.Fields{"meters": meters}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 707 | |
| 708 | for _, meter := range meters { |
| 709 | if meterMod.MeterId == meter.Config.MeterId { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 710 | logger.Infow("Meter-already-exists", log.Fields{"meter": *meterMod}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 711 | return nil |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | meterEntry := fu.MeterEntryFromMeterMod(meterMod) |
| 716 | meters = append(meters, meterEntry) |
| 717 | //Update model |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 718 | if err := agent.updateLogicalDeviceMetersWithoutLock(ctx, &ofp.Meters{Items: meters}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 719 | logger.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 720 | return err |
| 721 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 722 | logger.Debugw("Meter-added-successfully", log.Fields{"Added-meter": meterEntry, "updated-meters": lDevice.Meters}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 723 | return nil |
| 724 | } |
| 725 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 726 | func (agent *LogicalDeviceAgent) meterDelete(ctx context.Context, meterMod *ofp.OfpMeterMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 727 | logger.Debug("meterDelete", log.Fields{"meterMod": *meterMod}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 728 | if meterMod == nil { |
| 729 | return nil |
| 730 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 731 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 732 | return err |
| 733 | } |
| 734 | defer agent.requestQueue.RequestComplete() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 735 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 736 | lDevice := agent.getLogicalDeviceWithoutLock() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 737 | |
| 738 | var meters []*ofp.OfpMeterEntry |
| 739 | var flows []*ofp.OfpFlowStats |
| 740 | updatedFlows := make([]*ofp.OfpFlowStats, 0) |
| 741 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 742 | meters = lDevice.Meters.Items |
| 743 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 744 | |
| 745 | changedMeter := false |
| 746 | changedFow := false |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 747 | logger.Debugw("Available meters", log.Fields{"meters": meters}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 748 | for index, meter := range meters { |
| 749 | if meterMod.MeterId == meter.Config.MeterId { |
| 750 | flows = lDevice.Flows.Items |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 751 | changedFow, updatedFlows = agent.getUpdatedFlowsAfterDeletebyMeterID(flows, meterMod.MeterId) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 752 | meters = append(meters[:index], meters[index+1:]...) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 753 | logger.Debugw("Meter has been deleted", log.Fields{"meter": meter, "index": index}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 754 | changedMeter = true |
| 755 | break |
| 756 | } |
| 757 | } |
| 758 | if changedMeter { |
| 759 | //Update model |
| 760 | metersToUpdate := &ofp.Meters{} |
| 761 | if lDevice.Meters != nil { |
| 762 | metersToUpdate = &ofp.Meters{Items: meters} |
| 763 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 764 | if err := agent.updateLogicalDeviceMetersWithoutLock(ctx, metersToUpdate); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 765 | logger.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 766 | return err |
| 767 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 768 | logger.Debug("Meter-deleted-from-DB-successfully", log.Fields{"updatedMeters": metersToUpdate, "no-of-meter": len(metersToUpdate.Items)}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 769 | |
| 770 | } |
| 771 | if changedFow { |
| 772 | //Update model |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 773 | if err := agent.updateLogicalDeviceFlowsWithoutLock(ctx, &ofp.Flows{Items: updatedFlows}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 774 | logger.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 775 | return err |
| 776 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 777 | logger.Debug("Flows-associated-with-meter-deleted-from-DB-successfully", |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 778 | log.Fields{"updated-no-of-flows": len(updatedFlows), "meter": meterMod.MeterId}) |
| 779 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 780 | logger.Debugw("meterDelete success", log.Fields{"meterID": meterMod.MeterId}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 781 | return nil |
| 782 | } |
| 783 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 784 | func (agent *LogicalDeviceAgent) meterModify(ctx context.Context, meterMod *ofp.OfpMeterMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 785 | logger.Debug("meterModify") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 786 | if meterMod == nil { |
| 787 | return nil |
| 788 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 789 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 790 | return err |
| 791 | } |
| 792 | defer agent.requestQueue.RequestComplete() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 793 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 794 | lDevice := agent.getLogicalDeviceWithoutLock() |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 795 | |
| 796 | var meters []*ofp.OfpMeterEntry |
| 797 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 798 | meters = lDevice.Meters.Items |
| 799 | } |
| 800 | changedMeter := false |
| 801 | for index, meter := range meters { |
| 802 | if meterMod.MeterId == meter.Config.MeterId { |
| 803 | newmeterEntry := fu.MeterEntryFromMeterMod(meterMod) |
| 804 | newmeterEntry.Stats.FlowCount = meter.Stats.FlowCount |
| 805 | meters[index] = newmeterEntry |
| 806 | changedMeter = true |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 807 | logger.Debugw("Found meter, replaced with new meter", log.Fields{"old meter": meter, "new meter": newmeterEntry}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 808 | break |
| 809 | } |
| 810 | } |
| 811 | if changedMeter { |
| 812 | //Update model |
| 813 | metersToUpdate := &ofp.Meters{} |
| 814 | if lDevice.Meters != nil { |
| 815 | metersToUpdate = &ofp.Meters{Items: meters} |
| 816 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 817 | if err := agent.updateLogicalDeviceMetersWithoutLock(ctx, metersToUpdate); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 818 | logger.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 819 | return err |
| 820 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 821 | logger.Debugw("meter-updated-in-DB-successfully", log.Fields{"updated_meters": meters}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 822 | return nil |
| 823 | } |
| 824 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 825 | logger.Errorw("Meter not found ", log.Fields{"meter": meterMod}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 826 | return fmt.Errorf("no-logical-device-present:%d", meterMod.MeterId) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 827 | |
| 828 | } |
| 829 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 830 | func (agent *LogicalDeviceAgent) getUpdatedFlowsAfterDeletebyMeterID(flows []*ofp.OfpFlowStats, meterID uint32) (bool, []*ofp.OfpFlowStats) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 831 | logger.Infow("Delete flows matching meter", log.Fields{"meter": meterID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 832 | changed := false |
| 833 | //updatedFlows := make([]*ofp.OfpFlowStats, 0) |
| 834 | for index := len(flows) - 1; index >= 0; index-- { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 835 | if mID := fu.GetMeterIdFromFlow(flows[index]); mID != 0 && mID == meterID { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 836 | logger.Debugw("Flow to be deleted", log.Fields{"flow": flows[index], "index": index}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 837 | flows = append(flows[:index], flows[index+1:]...) |
| 838 | changed = true |
| 839 | } |
| 840 | } |
| 841 | return changed, flows |
| 842 | } |
| 843 | |
| 844 | func (agent *LogicalDeviceAgent) updateFlowCountOfMeterStats(modCommand *ofp.OfpFlowMod, meters []*ofp.OfpMeterEntry, flow *ofp.OfpFlowStats) bool { |
| 845 | |
| 846 | flowCommand := modCommand.GetCommand() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 847 | meterID := fu.GetMeterIdFromFlow(flow) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 848 | logger.Debugw("Meter-id-in-flow-mod", log.Fields{"meterId": meterID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 849 | if meterID == 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 850 | logger.Debugw("No meter present in the flow", log.Fields{"flow": *flow}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 851 | return false |
| 852 | } |
| 853 | if meters == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 854 | logger.Debug("No meters present in logical device") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 855 | return false |
| 856 | } |
| 857 | changedMeter := false |
| 858 | for _, meter := range meters { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 859 | if meterID == meter.Config.MeterId { // Found meter in Logicaldevice |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 860 | if flowCommand == ofp.OfpFlowModCommand_OFPFC_ADD { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 861 | meter.Stats.FlowCount++ |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 862 | changedMeter = true |
| 863 | } else if flowCommand == ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 864 | meter.Stats.FlowCount-- |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 865 | changedMeter = true |
| 866 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 867 | logger.Debugw("Found meter, updated meter flow stats", log.Fields{" meterId": meterID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 868 | break |
| 869 | } |
| 870 | } |
| 871 | return changedMeter |
| 872 | } |
| 873 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 874 | //flowAdd adds a flow to the flow table of that logical device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 875 | func (agent *LogicalDeviceAgent) flowAdd(ctx context.Context, mod *ofp.OfpFlowMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 876 | logger.Debugw("flowAdd", log.Fields{"flow": mod}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 877 | if mod == nil { |
| 878 | return nil |
| 879 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 880 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 881 | return err |
| 882 | } |
| 883 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 884 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 885 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 886 | |
| 887 | var flows []*ofp.OfpFlowStats |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 888 | var meters []*ofp.OfpMeterEntry |
| 889 | var flow *ofp.OfpFlowStats |
Scott Baker | fdea1e3 | 2020-02-21 15:35:41 -0800 | [diff] [blame] | 890 | var err error |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 891 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 892 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 893 | flows = lDevice.Flows.Items |
| 894 | } |
| 895 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 896 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 897 | meters = lDevice.Meters.Items |
| 898 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 899 | updatedFlows := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 900 | changed := false |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 901 | updated := false |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 902 | checkOverlap := (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_CHECK_OVERLAP)) != 0 |
| 903 | if checkOverlap { |
| 904 | if overlapped := fu.FindOverlappingFlows(flows, mod); len(overlapped) != 0 { |
| 905 | // TODO: should this error be notified other than being logged? |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 906 | logger.Warnw("overlapped-flows", log.Fields{"logicaldeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 907 | } else { |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 908 | // Add flow |
Scott Baker | fdea1e3 | 2020-02-21 15:35:41 -0800 | [diff] [blame] | 909 | flow, err = fu.FlowStatsEntryFromFlowModMessage(mod) |
| 910 | if err != nil { |
| 911 | return err |
| 912 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 913 | flows = append(flows, flow) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 914 | updatedFlows = append(updatedFlows, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 915 | changed = true |
| 916 | } |
| 917 | } else { |
Scott Baker | fdea1e3 | 2020-02-21 15:35:41 -0800 | [diff] [blame] | 918 | flow, err = fu.FlowStatsEntryFromFlowModMessage(mod) |
| 919 | if err != nil { |
| 920 | return err |
| 921 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 922 | idx := fu.FindFlows(flows, flow) |
| 923 | if idx >= 0 { |
| 924 | oldFlow := flows[idx] |
| 925 | if (mod.Flags & uint32(ofp.OfpFlowModFlags_OFPFF_RESET_COUNTS)) != 0 { |
| 926 | flow.ByteCount = oldFlow.ByteCount |
| 927 | flow.PacketCount = oldFlow.PacketCount |
| 928 | } |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 929 | if !reflect.DeepEqual(oldFlow, flow) { |
| 930 | flows[idx] = flow |
| 931 | updatedFlows = append(updatedFlows, flow) |
| 932 | changed = true |
| 933 | updated = true |
| 934 | } |
| 935 | } else { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 936 | flows = append(flows, flow) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 937 | updatedFlows = append(updatedFlows, flow) |
| 938 | changed = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 939 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 940 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 941 | logger.Debugw("flowAdd-changed", log.Fields{"changed": changed}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 942 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 943 | if changed { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 944 | var flowMetadata voltha.FlowMetadata |
| 945 | if err := agent.GetMeterConfig(updatedFlows, meters, &flowMetadata); err != nil { // This should never happen,meters should be installed before flow arrives |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 946 | logger.Error("Meter-referred-in-flows-not-present") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 947 | return err |
| 948 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 949 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, ofp.Flows{Items: updatedFlows}, *lDevice.FlowGroups) |
| 950 | if err != nil { |
| 951 | return err |
| 952 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 953 | logger.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 954 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 955 | // Update model |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 956 | if err := agent.updateLogicalDeviceFlowsWithoutLock(ctx, &ofp.Flows{Items: flows}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 957 | logger.Errorw("db-flow-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 958 | return err |
| 959 | } |
Matt Jeanneret | 518b5a4 | 2019-10-29 10:30:46 -0400 | [diff] [blame] | 960 | if !updated { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 961 | changedMeterStats := agent.updateFlowCountOfMeterStats(mod, meters, flow) |
| 962 | metersToUpdate := &ofp.Meters{} |
| 963 | if lDevice.Meters != nil { |
| 964 | metersToUpdate = &ofp.Meters{Items: meters} |
| 965 | } |
| 966 | if changedMeterStats { |
| 967 | //Update model |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 968 | if err := agent.updateLogicalDeviceMetersWithoutLock(ctx, metersToUpdate); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 969 | logger.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 970 | return err |
| 971 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 972 | logger.Debugw("meter-stats-updated-in-DB-successfully", log.Fields{"updated_meters": meters}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 973 | |
| 974 | } |
| 975 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 976 | // Send the flows to the devices |
| 977 | respChannels := agent.addFlowsAndGroupsToDevices(ctx, deviceRules, &flowMetadata) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 978 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 979 | // Create the go routines to wait |
| 980 | go func() { |
| 981 | // Wait for completion |
| 982 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChannels...); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 983 | logger.Warnw("failure-to-add-flows", log.Fields{"errors": res, "logical-device-id": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 984 | // TODO : revert added flow |
| 985 | } |
| 986 | }() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 987 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 988 | return nil |
| 989 | } |
| 990 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 991 | // GetMeterConfig returns meter config |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 992 | func (agent *LogicalDeviceAgent) GetMeterConfig(flows []*ofp.OfpFlowStats, meters []*ofp.OfpMeterEntry, metadata *voltha.FlowMetadata) error { |
| 993 | m := make(map[uint32]bool) |
| 994 | for _, flow := range flows { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 995 | if flowMeterID := fu.GetMeterIdFromFlow(flow); flowMeterID != 0 && !m[flowMeterID] { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 996 | foundMeter := false |
| 997 | // Meter is present in the flow , Get from logical device |
| 998 | for _, meter := range meters { |
| 999 | if flowMeterID == meter.Config.MeterId { |
| 1000 | metadata.Meters = append(metadata.Meters, meter.Config) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1001 | logger.Debugw("Found meter in logical device", |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1002 | log.Fields{"meterID": flowMeterID, "meter-band": meter.Config}) |
| 1003 | m[flowMeterID] = true |
| 1004 | foundMeter = true |
| 1005 | break |
| 1006 | } |
| 1007 | } |
| 1008 | if !foundMeter { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1009 | logger.Errorw("Meter-referred-by-flow-is-not-found-in-logicaldevice", |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1010 | log.Fields{"meterID": flowMeterID, "Available-meters": meters, "flow": *flow}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1011 | return errors.New("Meter-referred-by-flow-is-not-found-in-logicaldevice") |
| 1012 | } |
| 1013 | } |
| 1014 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1015 | logger.Debugw("meter-bands-for-flows", log.Fields{"flows": len(flows), "metadata": metadata}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1016 | return nil |
| 1017 | |
| 1018 | } |
| 1019 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1020 | //flowDelete deletes a flow from the flow table of that logical device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1021 | func (agent *LogicalDeviceAgent) flowDelete(ctx context.Context, mod *ofp.OfpFlowMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1022 | logger.Debug("flowDelete") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1023 | if mod == nil { |
| 1024 | return nil |
| 1025 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1026 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1027 | return err |
| 1028 | } |
| 1029 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1030 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1031 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1032 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1033 | var meters []*ofp.OfpMeterEntry |
| 1034 | var flows []*ofp.OfpFlowStats |
Esin Karaman | fc3b1af | 2020-01-29 16:52:00 +0000 | [diff] [blame] | 1035 | var flowGroups []*ofp.OfpGroupEntry |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1036 | |
| 1037 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 1038 | flows = lDevice.Flows.Items |
| 1039 | } |
| 1040 | |
| 1041 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 1042 | meters = lDevice.Meters.Items |
| 1043 | } |
Esin Karaman | fc3b1af | 2020-01-29 16:52:00 +0000 | [diff] [blame] | 1044 | |
| 1045 | if lDevice.FlowGroups != nil && lDevice.FlowGroups.Items != nil { |
| 1046 | flowGroups = lDevice.FlowGroups.Items |
| 1047 | } |
| 1048 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1049 | //build a list of what to keep vs what to delete |
| 1050 | toKeep := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1051 | toDelete := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1052 | for _, f := range flows { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1053 | // Check whether the flow and the flowmod matches |
Scott Baker | fdea1e3 | 2020-02-21 15:35:41 -0800 | [diff] [blame] | 1054 | fs, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 1055 | if err != nil { |
| 1056 | return err |
| 1057 | } |
| 1058 | if fu.FlowMatch(f, fs) { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1059 | toDelete = append(toDelete, f) |
| 1060 | continue |
| 1061 | } |
| 1062 | // Check wild card match |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1063 | if !fu.FlowMatchesMod(f, mod) { |
| 1064 | toKeep = append(toKeep, f) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1065 | } else { |
| 1066 | toDelete = append(toDelete, f) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1070 | logger.Debugw("flowDelete", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "toKeep": len(toKeep), "toDelete": toDelete}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1071 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1072 | //Update flows |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1073 | if len(toDelete) > 0 { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1074 | var flowMetadata voltha.FlowMetadata |
| 1075 | if err := agent.GetMeterConfig(toDelete, meters, &flowMetadata); err != nil { // This should never happen |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1076 | logger.Error("Meter-referred-in-flows-not-present") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1077 | return errors.New("Meter-referred-in-flows-not-present") |
| 1078 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1079 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, ofp.Flows{Items: toDelete}, ofp.FlowGroups{Items: flowGroups}) |
| 1080 | if err != nil { |
| 1081 | return err |
| 1082 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1083 | logger.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1084 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1085 | if err := agent.updateLogicalDeviceFlowsWithoutLock(ctx, &ofp.Flows{Items: toKeep}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1086 | logger.Errorw("cannot-update-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1087 | return err |
| 1088 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1089 | |
| 1090 | // Update the devices |
| 1091 | respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, &flowMetadata) |
| 1092 | |
| 1093 | // Wait for the responses |
| 1094 | go func() { |
| 1095 | // Wait for completion |
| 1096 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1097 | logger.Errorw("failure-updating-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1098 | // TODO: Revert the flow deletion |
| 1099 | } |
| 1100 | }() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | //TODO: send announcement on delete |
| 1104 | return nil |
| 1105 | } |
| 1106 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1107 | func (agent *LogicalDeviceAgent) addFlowsAndGroupsToDevices(ctx context.Context, deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) []coreutils.Response { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1108 | logger.Debugw("send-add-flows-to-device-manager", log.Fields{"logicalDeviceID": agent.logicalDeviceID, "deviceRules": deviceRules, "flowMetadata": flowMetadata}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1109 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1110 | responses := make([]coreutils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1111 | for deviceID, value := range deviceRules.GetRules() { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1112 | response := coreutils.NewResponse() |
| 1113 | responses = append(responses, response) |
| 1114 | go func(deviceId string, value *fu.FlowsAndGroups) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1115 | ctx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1116 | defer cancel() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1117 | if err := agent.deviceMgr.addFlowsAndGroups(ctx, deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1118 | logger.Errorw("flow-add-failed", log.Fields{"deviceID": deviceId, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1119 | response.Error(status.Errorf(codes.Internal, "flow-add-failed: %s", deviceId)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1120 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1121 | response.Done() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1122 | }(deviceID, value) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1123 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1124 | // Return responses (an array of channels) for the caller to wait for a response from the far end. |
| 1125 | return responses |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1126 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1127 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1128 | func (agent *LogicalDeviceAgent) deleteFlowsAndGroupsFromDevices(ctx context.Context, deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) []coreutils.Response { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1129 | logger.Debugw("send-delete-flows-to-device-manager", log.Fields{"logicalDeviceID": agent.logicalDeviceID}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1130 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1131 | responses := make([]coreutils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1132 | for deviceID, value := range deviceRules.GetRules() { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1133 | response := coreutils.NewResponse() |
| 1134 | responses = append(responses, response) |
| 1135 | go func(deviceId string, value *fu.FlowsAndGroups) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1136 | ctx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1137 | defer cancel() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1138 | if err := agent.deviceMgr.deleteFlowsAndGroups(ctx, deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1139 | logger.Error("flow-delete-failed", log.Fields{"deviceID": deviceId, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1140 | response.Error(status.Errorf(codes.Internal, "flow-delete-failed: %s", deviceId)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1141 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1142 | response.Done() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1143 | }(deviceID, value) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1144 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1145 | return responses |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1146 | } |
| 1147 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1148 | func (agent *LogicalDeviceAgent) updateFlowsAndGroupsOfDevice(ctx context.Context, deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) []coreutils.Response { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1149 | logger.Debugw("send-update-flows-to-device-manager", log.Fields{"logicalDeviceID": agent.logicalDeviceID}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1150 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1151 | responses := make([]coreutils.Response, 0) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1152 | for deviceID, value := range deviceRules.GetRules() { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1153 | response := coreutils.NewResponse() |
| 1154 | responses = append(responses, response) |
| 1155 | go func(deviceId string, value *fu.FlowsAndGroups) { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1156 | ctx, cancel := context.WithTimeout(context.Background(), agent.defaultTimeout) |
| 1157 | defer cancel() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1158 | if err := agent.deviceMgr.updateFlowsAndGroups(ctx, deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1159 | logger.Error("flow-update-failed", log.Fields{"deviceID": deviceId, "error": err}) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1160 | response.Error(status.Errorf(codes.Internal, "flow-update-failed: %s", deviceId)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1161 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 1162 | response.Done() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1163 | }(deviceID, value) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1164 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1165 | return responses |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | //flowDeleteStrict deletes a flow from the flow table of that logical device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1169 | func (agent *LogicalDeviceAgent) flowDeleteStrict(ctx context.Context, mod *ofp.OfpFlowMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1170 | logger.Debug("flowDeleteStrict") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1171 | if mod == nil { |
| 1172 | return nil |
| 1173 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1174 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1175 | return err |
| 1176 | } |
| 1177 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1178 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1179 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1180 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1181 | var meters []*ofp.OfpMeterEntry |
| 1182 | var flows []*ofp.OfpFlowStats |
Esin Karaman | fc3b1af | 2020-01-29 16:52:00 +0000 | [diff] [blame] | 1183 | var flowGroups []*ofp.OfpGroupEntry |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1184 | if lDevice.Meters != nil && lDevice.Meters.Items != nil { |
| 1185 | meters = lDevice.Meters.Items |
| 1186 | } |
| 1187 | if lDevice.Flows != nil && lDevice.Flows.Items != nil { |
| 1188 | flows = lDevice.Flows.Items |
| 1189 | } |
Esin Karaman | fc3b1af | 2020-01-29 16:52:00 +0000 | [diff] [blame] | 1190 | if lDevice.FlowGroups != nil && lDevice.FlowGroups.Items != nil { |
| 1191 | flowGroups = lDevice.FlowGroups.Items |
| 1192 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1193 | |
| 1194 | changedFlow := false |
| 1195 | changedMeter := false |
Scott Baker | fdea1e3 | 2020-02-21 15:35:41 -0800 | [diff] [blame] | 1196 | flow, err := fu.FlowStatsEntryFromFlowModMessage(mod) |
| 1197 | if err != nil { |
| 1198 | return err |
| 1199 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1200 | flowsToDelete := make([]*ofp.OfpFlowStats, 0) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1201 | idx := fu.FindFlows(flows, flow) |
| 1202 | if idx >= 0 { |
Gamze Abaka | 6e4ac16 | 2019-10-21 11:10:10 +0000 | [diff] [blame] | 1203 | changedMeter = agent.updateFlowCountOfMeterStats(mod, meters, flows[idx]) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1204 | flowsToDelete = append(flowsToDelete, flows[idx]) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1205 | flows = append(flows[:idx], flows[idx+1:]...) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1206 | changedFlow = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1207 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1208 | return fmt.Errorf("Cannot delete flow - %s", flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1209 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1210 | if changedMeter { |
| 1211 | //Update model |
| 1212 | metersToUpdate := &ofp.Meters{} |
| 1213 | if lDevice.Meters != nil { |
| 1214 | metersToUpdate = &ofp.Meters{Items: meters} |
| 1215 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1216 | if err := agent.updateLogicalDeviceMetersWithoutLock(ctx, metersToUpdate); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1217 | logger.Errorw("db-meter-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1218 | return err |
| 1219 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1220 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1221 | } |
| 1222 | if changedFlow { |
| 1223 | var flowMetadata voltha.FlowMetadata |
| 1224 | if err := agent.GetMeterConfig(flowsToDelete, meters, &flowMetadata); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1225 | logger.Error("meter-referred-in-flows-not-present") |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 1226 | return err |
| 1227 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1228 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, ofp.Flows{Items: flowsToDelete}, ofp.FlowGroups{Items: flowGroups}) |
| 1229 | if err != nil { |
| 1230 | return err |
| 1231 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1232 | logger.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1233 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1234 | if err := agent.updateLogicalDeviceFlowsWithoutLock(ctx, &ofp.Flows{Items: flows}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1235 | logger.Errorw("cannot-update-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1236 | return err |
| 1237 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1238 | |
| 1239 | // Update the devices |
| 1240 | respChnls := agent.deleteFlowsAndGroupsFromDevices(ctx, deviceRules, &flowMetadata) |
| 1241 | |
| 1242 | // Wait for completion |
| 1243 | go func() { |
| 1244 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1245 | logger.Warnw("failure-deleting-device-flows", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1246 | //TODO: Revert flow changes |
| 1247 | } |
| 1248 | }() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1249 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1250 | return nil |
| 1251 | } |
| 1252 | |
| 1253 | //flowModify modifies a flow from the flow table of that logical device |
| 1254 | func (agent *LogicalDeviceAgent) flowModify(mod *ofp.OfpFlowMod) error { |
| 1255 | return errors.New("flowModify not implemented") |
| 1256 | } |
| 1257 | |
| 1258 | //flowModifyStrict deletes a flow from the flow table of that logical device |
| 1259 | func (agent *LogicalDeviceAgent) flowModifyStrict(mod *ofp.OfpFlowMod) error { |
| 1260 | return errors.New("flowModifyStrict not implemented") |
| 1261 | } |
| 1262 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1263 | func (agent *LogicalDeviceAgent) groupAdd(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1264 | logger.Debug("groupAdd") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1265 | if groupMod == nil { |
| 1266 | return nil |
| 1267 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1268 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1269 | return err |
| 1270 | } |
| 1271 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1272 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1273 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1274 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1275 | groups := lDevice.FlowGroups.Items |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1276 | if fu.FindGroup(groups, groupMod.GroupId) == -1 { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1277 | groups = append(groups, fu.GroupEntryFromGroupMod(groupMod)) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1278 | |
Esin Karaman | 2ea5921 | 2019-12-06 11:41:58 +0000 | [diff] [blame] | 1279 | deviceRules := fu.NewDeviceRules() |
| 1280 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
| 1281 | fg := fu.NewFlowsAndGroups() |
| 1282 | fg.AddGroup(fu.GroupEntryFromGroupMod(groupMod)) |
| 1283 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
| 1284 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1285 | logger.Debugw("rules", log.Fields{"rules for group-add": deviceRules.String()}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1286 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1287 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(ctx, &ofp.FlowGroups{Items: groups}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1288 | logger.Errorw("cannot-update-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1289 | return err |
| 1290 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1291 | |
| 1292 | // Update the devices |
| 1293 | respChnls := agent.addFlowsAndGroupsToDevices(ctx, deviceRules, &voltha.FlowMetadata{}) |
| 1294 | |
| 1295 | // Wait for completion |
| 1296 | go func() { |
| 1297 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1298 | logger.Warnw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1299 | //TODO: Revert flow changes |
| 1300 | } |
| 1301 | }() |
| 1302 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1303 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1304 | return fmt.Errorf("Groups %d already present", groupMod.GroupId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1307 | func (agent *LogicalDeviceAgent) groupDelete(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1308 | logger.Debug("groupDelete") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1309 | if groupMod == nil { |
| 1310 | return nil |
| 1311 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1312 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1313 | return err |
| 1314 | } |
| 1315 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1316 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1317 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1318 | groups := lDevice.FlowGroups.Items |
| 1319 | flows := lDevice.Flows.Items |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1320 | var groupsChanged bool |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1321 | flowsChanged := false |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1322 | groupID := groupMod.GroupId |
| 1323 | if groupID == uint32(ofp.OfpGroup_OFPG_ALL) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1324 | //TODO we must delete all flows that point to this group and |
| 1325 | //signal controller as requested by flow's flag |
| 1326 | groups = []*ofp.OfpGroupEntry{} |
| 1327 | groupsChanged = true |
| 1328 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1329 | idx := fu.FindGroup(groups, groupID) |
| 1330 | if idx == -1 { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1331 | return nil // Valid case |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1332 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1333 | flowsChanged, flows = fu.FlowsDeleteByGroupId(flows, groupID) |
| 1334 | groups = append(groups[:idx], groups[idx+1:]...) |
| 1335 | groupsChanged = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1336 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1337 | if flowsChanged || groupsChanged { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1338 | deviceRules, err := agent.flowDecomposer.DecomposeRules(ctx, agent, ofp.Flows{Items: flows}, ofp.FlowGroups{Items: groups}) |
| 1339 | if err != nil { |
| 1340 | return err |
| 1341 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1342 | logger.Debugw("rules", log.Fields{"rules": deviceRules.String()}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1343 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1344 | if groupsChanged { |
| 1345 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(ctx, &ofp.FlowGroups{Items: groups}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1346 | logger.Errorw("cannot-update-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1347 | return err |
| 1348 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1349 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1350 | if flowsChanged { |
| 1351 | if err := agent.updateLogicalDeviceFlowsWithoutLock(ctx, &ofp.Flows{Items: flows}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1352 | logger.Errorw("cannot-update-flow", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1353 | return err |
| 1354 | } |
| 1355 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1356 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1357 | // Update the devices |
| 1358 | respChnls := agent.updateFlowsAndGroupsOfDevice(ctx, deviceRules, nil) |
| 1359 | |
| 1360 | // Wait for completion |
| 1361 | go func() { |
| 1362 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1363 | logger.Warnw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1364 | //TODO: Revert flow changes |
| 1365 | } |
| 1366 | }() |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 1367 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1368 | return nil |
| 1369 | } |
| 1370 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1371 | func (agent *LogicalDeviceAgent) groupModify(ctx context.Context, groupMod *ofp.OfpGroupMod) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1372 | logger.Debug("groupModify") |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1373 | if groupMod == nil { |
| 1374 | return nil |
| 1375 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1376 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1377 | return err |
| 1378 | } |
| 1379 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1380 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1381 | lDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1382 | groups := lDevice.FlowGroups.Items |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1383 | var groupsChanged bool |
| 1384 | groupID := groupMod.GroupId |
| 1385 | idx := fu.FindGroup(groups, groupID) |
| 1386 | if idx == -1 { |
| 1387 | return fmt.Errorf("group-absent:%d", groupID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1388 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1389 | //replace existing group entry with new group definition |
| 1390 | groupEntry := fu.GroupEntryFromGroupMod(groupMod) |
| 1391 | groups[idx] = groupEntry |
| 1392 | groupsChanged = true |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1393 | if groupsChanged { |
Esin Karaman | 2ea5921 | 2019-12-06 11:41:58 +0000 | [diff] [blame] | 1394 | deviceRules := fu.NewDeviceRules() |
| 1395 | deviceRules.CreateEntryIfNotExist(agent.rootDeviceID) |
| 1396 | fg := fu.NewFlowsAndGroups() |
| 1397 | fg.AddGroup(fu.GroupEntryFromGroupMod(groupMod)) |
| 1398 | deviceRules.AddFlowsAndGroup(agent.rootDeviceID, fg) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1399 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1400 | logger.Debugw("rules", log.Fields{"rules for group-modify": deviceRules.String()}) |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1401 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1402 | if err := agent.updateLogicalDeviceFlowGroupsWithoutLock(ctx, &ofp.FlowGroups{Items: groups}); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1403 | logger.Errorw("Cannot-update-logical-group", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1404 | return err |
| 1405 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1406 | |
| 1407 | // Update the devices |
| 1408 | respChnls := agent.updateFlowsAndGroupsOfDevice(ctx, deviceRules, &voltha.FlowMetadata{}) |
| 1409 | |
| 1410 | // Wait for completion |
| 1411 | go func() { |
| 1412 | if res := coreutils.WaitForNilOrErrorResponses(agent.defaultTimeout, respChnls...); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1413 | logger.Warnw("failure-updating-device-flows-groups", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "errors": res}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1414 | //TODO: Revert flow changes |
| 1415 | } |
| 1416 | }() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1417 | } |
| 1418 | return nil |
| 1419 | } |
| 1420 | |
| 1421 | // deleteLogicalPort removes the logical port |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1422 | func (agent *LogicalDeviceAgent) deleteLogicalPort(ctx context.Context, lPort *voltha.LogicalPort) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1423 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1424 | return err |
| 1425 | } |
| 1426 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1427 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1428 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
| 1429 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1430 | index := -1 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1431 | for i, logicalPort := range logicalDevice.Ports { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1432 | if logicalPort.Id == lPort.Id { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1433 | index = i |
| 1434 | break |
| 1435 | } |
| 1436 | } |
| 1437 | if index >= 0 { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1438 | copy(logicalDevice.Ports[index:], logicalDevice.Ports[index+1:]) |
| 1439 | logicalDevice.Ports[len(logicalDevice.Ports)-1] = nil |
| 1440 | logicalDevice.Ports = logicalDevice.Ports[:len(logicalDevice.Ports)-1] |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1441 | logger.Debugw("logical-port-deleted", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1442 | if err := agent.updateLogicalDeviceWithoutLock(ctx, logicalDevice); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1443 | logger.Errorw("logical-device-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1444 | return err |
| 1445 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1446 | |
| 1447 | // Remove the logical port from cache |
| 1448 | agent.deleteLogicalPortsFromMap([]uint32{lPort.DevicePortNo}) |
| 1449 | |
| 1450 | // Reset the logical device routes |
| 1451 | go func() { |
| 1452 | if err := agent.buildRoutes(context.Background()); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1453 | logger.Warnw("device-routes-not-ready", log.Fields{"logicalDeviceId": agent.logicalDeviceID, "error": err}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1454 | } |
| 1455 | }() |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 1456 | } |
| 1457 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1458 | } |
| 1459 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1460 | // deleteLogicalPorts removes the logical ports associated with that deviceId |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1461 | func (agent *LogicalDeviceAgent) deleteLogicalPorts(ctx context.Context, deviceID string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1462 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1463 | return err |
| 1464 | } |
| 1465 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1466 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1467 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1468 | lPortstoKeep := []*voltha.LogicalPort{} |
| 1469 | lPortsNoToDelete := []uint32{} |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1470 | for _, logicalPort := range logicalDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1471 | if logicalPort.DeviceId != deviceID { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1472 | lPortstoKeep = append(lPortstoKeep, logicalPort) |
| 1473 | } else { |
| 1474 | lPortsNoToDelete = append(lPortsNoToDelete, logicalPort.DevicePortNo) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1475 | } |
| 1476 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1477 | logicalDevice.Ports = lPortstoKeep |
| 1478 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1479 | logger.Debugw("updated-logical-ports", log.Fields{"ports": lPortstoKeep}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1480 | if err := agent.updateLogicalDeviceWithoutLock(ctx, logicalDevice); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1481 | logger.Errorw("logical-device-update-failed", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1482 | return err |
| 1483 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1484 | // Remove the port from the cached logical ports set |
| 1485 | agent.deleteLogicalPortsFromMap(lPortsNoToDelete) |
| 1486 | |
| 1487 | // Reset the logical device routes |
| 1488 | go func() { |
| 1489 | if err := agent.buildRoutes(context.Background()); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1490 | logger.Warnw("routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1491 | } |
| 1492 | }() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1493 | |
| 1494 | return nil |
| 1495 | } |
| 1496 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1497 | // enableLogicalPort enables the logical port |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1498 | func (agent *LogicalDeviceAgent) enableLogicalPort(ctx context.Context, lPortID string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1499 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1500 | return err |
| 1501 | } |
| 1502 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1503 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1504 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
| 1505 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1506 | index := -1 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1507 | for i, logicalPort := range logicalDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1508 | if logicalPort.Id == lPortID { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1509 | index = i |
| 1510 | break |
| 1511 | } |
| 1512 | } |
| 1513 | if index >= 0 { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1514 | logicalDevice.Ports[index].OfpPort.Config = logicalDevice.Ports[index].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1515 | return agent.updateLogicalDeviceWithoutLock(ctx, logicalDevice) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1516 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1517 | return status.Errorf(codes.NotFound, "Port %s on Logical Device %s", lPortID, agent.logicalDeviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | // disableLogicalPort disabled the logical port |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1521 | func (agent *LogicalDeviceAgent) disableLogicalPort(ctx context.Context, lPortID string) error { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1522 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1523 | return err |
| 1524 | } |
| 1525 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1526 | |
| 1527 | // Get the most up to date logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1528 | logicalDevice := agent.getLogicalDeviceWithoutLock() |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1529 | index := -1 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1530 | for i, logicalPort := range logicalDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1531 | if logicalPort.Id == lPortID { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1532 | index = i |
| 1533 | break |
| 1534 | } |
| 1535 | } |
| 1536 | if index >= 0 { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1537 | logicalDevice.Ports[index].OfpPort.Config = (logicalDevice.Ports[index].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN)) | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1538 | return agent.updateLogicalDeviceWithoutLock(ctx, logicalDevice) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1539 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1540 | return status.Errorf(codes.NotFound, "Port %s on Logical Device %s", lPortID, agent.logicalDeviceID) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1541 | } |
| 1542 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1543 | func (agent *LogicalDeviceAgent) getPreCalculatedRoute(ingress, egress uint32) ([]route.Hop, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1544 | logger.Debugw("ROUTE", log.Fields{"len": len(agent.deviceRoutes.Routes)}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1545 | for routeLink, route := range agent.deviceRoutes.Routes { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1546 | logger.Debugw("ROUTELINKS", log.Fields{"ingress": ingress, "egress": egress, "routelink": routeLink}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1547 | if ingress == routeLink.Ingress && egress == routeLink.Egress { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1548 | return route, nil |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1549 | } |
| 1550 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1551 | return nil, status.Errorf(codes.FailedPrecondition, "no route from:%d to:%d", ingress, egress) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1552 | } |
| 1553 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1554 | // GetRoute returns route |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1555 | func (agent *LogicalDeviceAgent) GetRoute(ctx context.Context, ingressPortNo uint32, egressPortNo uint32) ([]route.Hop, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1556 | logger.Debugw("getting-route", log.Fields{"ingress-port": ingressPortNo, "egress-port": egressPortNo}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1557 | routes := make([]route.Hop, 0) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1558 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1559 | // Note: A port value of 0 is equivalent to a nil port |
| 1560 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1561 | // Consider different possibilities |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1562 | if egressPortNo != 0 && ((egressPortNo & 0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER)) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1563 | logger.Debugw("controller-flow", log.Fields{"ingressPortNo": ingressPortNo, "egressPortNo": egressPortNo, "logicalPortsNo": agent.logicalPortsNo}) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1564 | if agent.isNNIPort(ingressPortNo) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1565 | //This is a trap on the NNI Port |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1566 | if len(agent.deviceRoutes.Routes) == 0 { |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1567 | // If there are no routes set (usually when the logical device has only NNI port(s), then just return an |
Humera Kouser | 4ff8901 | 2019-08-25 19:01:51 -0400 | [diff] [blame] | 1568 | // route with same IngressHop and EgressHop |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1569 | hop := route.Hop{DeviceID: agent.rootDeviceID, Ingress: ingressPortNo, Egress: ingressPortNo} |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1570 | routes = append(routes, hop) |
| 1571 | routes = append(routes, hop) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1572 | return routes, nil |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1573 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1574 | //Return a 'half' route to make the flow decomposer logic happy |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1575 | for routeLink, path := range agent.deviceRoutes.Routes { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1576 | if agent.isNNIPort(routeLink.Egress) { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1577 | routes = append(routes, route.Hop{}) // first hop is set to empty |
| 1578 | routes = append(routes, path[1]) |
| 1579 | return routes, nil |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1580 | } |
| 1581 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1582 | return nil, status.Errorf(codes.FailedPrecondition, "no upstream route from:%d to:%d", ingressPortNo, egressPortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1583 | } |
| 1584 | //treat it as if the output port is the first NNI of the OLT |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1585 | var err error |
| 1586 | if egressPortNo, err = agent.getFirstNNIPort(); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1587 | logger.Warnw("no-nni-port", log.Fields{"error": err}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1588 | return nil, err |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1589 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1590 | } |
| 1591 | //If ingress port is not specified (nil), it may be a wildcarded |
| 1592 | //route if egress port is OFPP_CONTROLLER or a nni logical port, |
| 1593 | //in which case we need to create a half-route where only the egress |
| 1594 | //hop is filled, the first hop is nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1595 | if ingressPortNo == 0 && agent.isNNIPort(egressPortNo) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1596 | // We can use the 2nd hop of any upstream route, so just find the first upstream: |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1597 | for routeLink, path := range agent.deviceRoutes.Routes { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1598 | if agent.isNNIPort(routeLink.Egress) { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1599 | routes = append(routes, route.Hop{}) // first hop is set to empty |
| 1600 | routes = append(routes, path[1]) |
| 1601 | return routes, nil |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1602 | } |
| 1603 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1604 | return nil, status.Errorf(codes.FailedPrecondition, "no upstream route from:%d to:%d", ingressPortNo, egressPortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1605 | } |
| 1606 | //If egress port is not specified (nil), we can also can return a "half" route |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1607 | if egressPortNo == 0 { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1608 | for routeLink, path := range agent.deviceRoutes.Routes { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1609 | if routeLink.Ingress == ingressPortNo { |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1610 | routes = append(routes, path[0]) |
| 1611 | routes = append(routes, route.Hop{}) |
| 1612 | return routes, nil |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1613 | } |
| 1614 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1615 | return nil, status.Errorf(codes.FailedPrecondition, "no downstream route from:%d to:%d", ingressPortNo, egressPortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1616 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1617 | // Return the pre-calculated route |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1618 | return agent.getPreCalculatedRoute(ingressPortNo, egressPortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1619 | } |
| 1620 | |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 1621 | //GetWildcardInputPorts filters out the logical port number from the set of logical ports on the device and |
| 1622 | //returns their port numbers. This function is invoked only during flow decomposition where the lock on the logical |
| 1623 | //device is already held. Therefore it is safe to retrieve the logical device without lock. |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1624 | func (agent *LogicalDeviceAgent) GetWildcardInputPorts(excludePort ...uint32) []uint32 { |
| 1625 | lPorts := make([]uint32, 0) |
| 1626 | var exclPort uint32 |
| 1627 | if len(excludePort) == 1 { |
| 1628 | exclPort = excludePort[0] |
| 1629 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1630 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1631 | for _, port := range lDevice.Ports { |
| 1632 | if port.OfpPort.PortNo != exclPort { |
| 1633 | lPorts = append(lPorts, port.OfpPort.PortNo) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | return lPorts |
| 1637 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1638 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1639 | // GetDeviceRoutes returns device graph |
| 1640 | func (agent *LogicalDeviceAgent) GetDeviceRoutes() *route.DeviceRoutes { |
| 1641 | return agent.deviceRoutes |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1642 | } |
| 1643 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1644 | //rebuildRoutes rebuilds the device routes |
| 1645 | func (agent *LogicalDeviceAgent) buildRoutes(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1646 | logger.Debugf("building-routes", log.Fields{"logical-device-id": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1647 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1648 | return err |
| 1649 | } |
| 1650 | defer agent.requestQueue.RequestComplete() |
| 1651 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1652 | if agent.deviceRoutes == nil { |
| 1653 | agent.deviceRoutes = route.NewDeviceRoutes(agent.logicalDeviceID, agent.deviceMgr.GetDevice) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1654 | } |
| 1655 | // Get all the logical ports on that logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1656 | lDevice := agent.getLogicalDeviceWithoutLock() |
| 1657 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1658 | if err := agent.deviceRoutes.ComputeRoutes(ctx, lDevice.Ports); err != nil { |
| 1659 | return err |
| 1660 | } |
| 1661 | if err := agent.deviceRoutes.Print(); err != nil { |
| 1662 | return err |
| 1663 | } |
| 1664 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1665 | return nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1666 | } |
| 1667 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1668 | //updateRoutes updates the device routes |
| 1669 | func (agent *LogicalDeviceAgent) updateRoutes(ctx context.Context, lp *voltha.LogicalPort) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1670 | logger.Debugw("updateRoutes", log.Fields{"logicalDeviceId": agent.logicalDeviceID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1671 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1672 | return err |
| 1673 | } |
| 1674 | defer agent.requestQueue.RequestComplete() |
| 1675 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1676 | if agent.deviceRoutes == nil { |
| 1677 | agent.deviceRoutes = route.NewDeviceRoutes(agent.logicalDeviceID, agent.deviceMgr.GetDevice) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1678 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1679 | if err := agent.deviceRoutes.AddPort(ctx, lp, agent.logicalDevice.Ports); err != nil { |
| 1680 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1681 | } |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1682 | if err := agent.deviceRoutes.Print(); err != nil { |
| 1683 | return err |
| 1684 | } |
| 1685 | return nil |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1688 | // diff go over two lists of logical ports and return what's new, what's changed and what's removed. |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1689 | func diff(oldList, newList []*voltha.LogicalPort) (newPorts, changedPorts, deletedPorts []*voltha.LogicalPort) { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1690 | newPorts = make([]*voltha.LogicalPort, 0) |
| 1691 | changedPorts = make([]*voltha.LogicalPort, 0) |
| 1692 | deletedPorts = make([]*voltha.LogicalPort, 0) |
| 1693 | for _, o := range oldList { |
| 1694 | found := false |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1695 | for _, n := range newList { |
| 1696 | if o.Id == n.Id { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1697 | found = true |
| 1698 | break |
| 1699 | } |
| 1700 | } |
| 1701 | if !found { |
| 1702 | deletedPorts = append(deletedPorts, o) |
| 1703 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1704 | } |
| 1705 | for _, n := range newList { |
| 1706 | found := false |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 1707 | changed := false |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1708 | for _, o := range oldList { |
| 1709 | if o.Id == n.Id { |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 1710 | changed = !reflect.DeepEqual(o, n) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1711 | found = true |
| 1712 | break |
| 1713 | } |
| 1714 | } |
| 1715 | if !found { |
| 1716 | newPorts = append(newPorts, n) |
| 1717 | } |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 1718 | if changed { |
| 1719 | changedPorts = append(changedPorts, n) |
| 1720 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1721 | } |
| 1722 | return |
| 1723 | } |
| 1724 | |
| 1725 | // portUpdated is invoked when a port is updated on the logical device. Until |
| 1726 | // the POST_ADD notification is fixed, we will use the logical device to |
| 1727 | // update that data. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1728 | func (agent *LogicalDeviceAgent) portUpdated(ctx context.Context, args ...interface{}) interface{} { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1729 | logger.Debugw("portUpdated-callback", log.Fields{"argsLen": len(args)}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1730 | |
| 1731 | var oldLD *voltha.LogicalDevice |
| 1732 | var newlD *voltha.LogicalDevice |
| 1733 | |
| 1734 | var ok bool |
| 1735 | if oldLD, ok = args[0].(*voltha.LogicalDevice); !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1736 | logger.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1737 | return nil |
| 1738 | } |
| 1739 | if newlD, ok = args[1].(*voltha.LogicalDevice); !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1740 | logger.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1741 | return nil |
| 1742 | } |
| 1743 | |
| 1744 | if reflect.DeepEqual(oldLD.Ports, newlD.Ports) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1745 | logger.Debug("ports-have-not-changed") |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1746 | return nil |
| 1747 | } |
| 1748 | |
| 1749 | // Get the difference between the two list |
| 1750 | newPorts, changedPorts, deletedPorts := diff(oldLD.Ports, newlD.Ports) |
| 1751 | |
| 1752 | // Send the port change events to the OF controller |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1753 | for _, newP := range newPorts { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1754 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceID, |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1755 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_ADD, Desc: newP.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1756 | } |
| 1757 | for _, change := range changedPorts { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1758 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceID, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1759 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_MODIFY, Desc: change.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1760 | } |
| 1761 | for _, del := range deletedPorts { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1762 | go agent.ldeviceMgr.grpcNbiHdlr.sendChangeEvent(agent.logicalDeviceID, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1763 | &ofp.OfpPortStatus{Reason: ofp.OfpPortReason_OFPPR_DELETE, Desc: del.OfpPort}) |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | return nil |
| 1767 | } |
| 1768 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1769 | // addNNILogicalPort adds an NNI port to the logical device. It returns a bool representing whether a port has been |
| 1770 | // added and an eror in case a valid error is encountered. If the port was successfully added it will return |
| 1771 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 1772 | // scenario. This also applies to the case where the port was already added. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1773 | func (agent *LogicalDeviceAgent) addNNILogicalPort(ctx context.Context, device *voltha.Device, port *voltha.Port) (bool, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1774 | logger.Debugw("addNNILogicalPort", log.Fields{"NNI": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1775 | if device.AdminState != voltha.AdminState_ENABLED || device.OperStatus != voltha.OperStatus_ACTIVE { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1776 | logger.Infow("device-not-ready", log.Fields{"deviceId": device.Id, "admin": device.AdminState, "oper": device.OperStatus}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1777 | return false, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1778 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1779 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1780 | return false, err |
| 1781 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1782 | if agent.portExist(device, port) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1783 | logger.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1784 | agent.requestQueue.RequestComplete() |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1785 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1786 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1787 | agent.requestQueue.RequestComplete() |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1788 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1789 | var portCap *ic.PortCapability |
| 1790 | var err error |
| 1791 | // First get the port capability |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1792 | if portCap, err = agent.deviceMgr.getPortCapability(ctx, device.Id, port.PortNo); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1793 | logger.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1794 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1795 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1796 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1797 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1798 | return false, err |
| 1799 | } |
| 1800 | |
| 1801 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1802 | // Double check again if this port has been already added since the getPortCapability could have taken a long time |
| 1803 | if agent.portExist(device, port) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1804 | logger.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1805 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1806 | } |
| 1807 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1808 | portCap.Port.RootPort = true |
| 1809 | lp := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
| 1810 | lp.DeviceId = device.Id |
| 1811 | lp.Id = fmt.Sprintf("nni-%d", port.PortNo) |
| 1812 | lp.OfpPort.PortNo = port.PortNo |
| 1813 | lp.OfpPort.Name = lp.Id |
| 1814 | lp.DevicePortNo = port.PortNo |
| 1815 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1816 | ld := agent.getLogicalDeviceWithoutLock() |
| 1817 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1818 | cloned := (proto.Clone(ld)).(*voltha.LogicalDevice) |
| 1819 | if cloned.Ports == nil { |
| 1820 | cloned.Ports = make([]*voltha.LogicalPort, 0) |
| 1821 | } |
| 1822 | cloned.Ports = append(cloned.Ports, lp) |
| 1823 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1824 | if err = agent.updateLogicalDeviceWithoutLock(ctx, cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1825 | logger.Errorw("error-updating-logical-device", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1826 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1827 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1828 | |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1829 | // Update the device routes with this new logical port |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1830 | clonedLP := (proto.Clone(lp)).(*voltha.LogicalPort) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1831 | go func() { |
| 1832 | if err := agent.updateRoutes(context.Background(), clonedLP); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1833 | logger.Warnw("routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "logical-port": lp.OfpPort.PortNo, "error": err}) |
khenaidoo | 820197c | 2020-02-13 16:35:33 -0500 | [diff] [blame] | 1834 | } |
| 1835 | }() |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1836 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1837 | return true, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1838 | } |
| 1839 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 1840 | func (agent *LogicalDeviceAgent) portExist(device *voltha.Device, port *voltha.Port) bool { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1841 | ldevice := agent.getLogicalDeviceWithoutLock() |
| 1842 | for _, lPort := range ldevice.Ports { |
| 1843 | if lPort.DeviceId == device.Id && lPort.DevicePortNo == port.PortNo && lPort.Id == port.Label { |
| 1844 | return true |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | return false |
| 1848 | } |
| 1849 | |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1850 | // addUNILogicalPort adds an UNI port to the logical device. It returns a bool representing whether a port has been |
| 1851 | // added and an eror in case a valid error is encountered. If the port was successfully added it will return |
| 1852 | // (true, nil). If the device is not in the correct state it will return (false, nil) as this is a valid |
| 1853 | // scenario. This also applies to the case where the port was already added. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1854 | func (agent *LogicalDeviceAgent) addUNILogicalPort(ctx context.Context, childDevice *voltha.Device, port *voltha.Port) (bool, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1855 | logger.Debugw("addUNILogicalPort", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1856 | if childDevice.AdminState != voltha.AdminState_ENABLED || childDevice.OperStatus != voltha.OperStatus_ACTIVE { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1857 | logger.Infow("device-not-ready", log.Fields{"deviceId": childDevice.Id, "admin": childDevice.AdminState, "oper": childDevice.OperStatus}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1858 | return false, nil |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1859 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1860 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1861 | return false, err |
| 1862 | } |
| 1863 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1864 | if agent.portExist(childDevice, port) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1865 | logger.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1866 | agent.requestQueue.RequestComplete() |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1867 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1868 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1869 | agent.requestQueue.RequestComplete() |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1870 | var portCap *ic.PortCapability |
| 1871 | var err error |
| 1872 | // First get the port capability |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 1873 | if portCap, err = agent.deviceMgr.getPortCapability(ctx, childDevice.Id, port.PortNo); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1874 | logger.Errorw("error-retrieving-port-capabilities", log.Fields{"error": err}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1875 | return false, err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1876 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1877 | if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil { |
| 1878 | return false, err |
| 1879 | } |
| 1880 | defer agent.requestQueue.RequestComplete() |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1881 | // Double check again if this port has been already added since the getPortCapability could have taken a long time |
| 1882 | if agent.portExist(childDevice, port) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1883 | logger.Debugw("port-already-exist", log.Fields{"port": port}) |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 1884 | return false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1885 | } |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 1886 | // Get stored logical device |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 1887 | ldevice := agent.getLogicalDeviceWithoutLock() |
| 1888 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1889 | logger.Debugw("adding-uni", log.Fields{"deviceId": childDevice.Id}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 1890 | portCap.Port.RootPort = false |
| 1891 | portCap.Port.Id = port.Label |
| 1892 | portCap.Port.OfpPort.PortNo = port.PortNo |
| 1893 | portCap.Port.Device
|