blob: 4ce9849988f3509ca912c160547523b2e25ace60 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
Akash Sonia8246972023-01-03 10:37:08 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package application
17
18import (
Tinoj Joseph07cc5372022-07-18 22:53:51 +053019 "context"
Naveen Sampath04696f72022-06-13 15:19:14 +053020 "encoding/json"
21 "errors"
22 "net"
23 "reflect"
Naveen Sampath04696f72022-06-13 15:19:14 +053024 "strings"
25 "sync"
26 "time"
Akash Sonia8246972023-01-03 10:37:08 +053027 common "voltha-go-controller/internal/pkg/types"
Naveen Sampath04696f72022-06-13 15:19:14 +053028
29 "github.com/google/gopacket"
30 "github.com/google/gopacket/layers"
31
Naveen Sampath04696f72022-06-13 15:19:14 +053032 "voltha-go-controller/database"
Akash Sonia8246972023-01-03 10:37:08 +053033 cntlr "voltha-go-controller/internal/pkg/controller"
Naveen Sampath04696f72022-06-13 15:19:14 +053034 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053035 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053036)
37
38const (
39 // IgmpVersion0 constant (Default init value)
40 IgmpVersion0 uint8 = 0
41 // IgmpVersion1 constant
42 IgmpVersion1 uint8 = 1
43 // IgmpVersion2 constant
44 IgmpVersion2 uint8 = 2
45 // IgmpVersion3 constant
46 IgmpVersion3 uint8 = 3
47 // MinKeepAliveInterval constant
48 MinKeepAliveInterval uint32 = 10
49 // MaxDiffKAIntervalResp constant
50 MaxDiffKAIntervalResp uint32 = 5
51 // StaticGroup constant
52 StaticGroup string = "static"
53 // DynamicGroup constant
54 DynamicGroup string = "dynamic"
55 // StaticPort constant
56 StaticPort string = "static_port"
57 // DefaultIgmpProfID constant
58 DefaultIgmpProfID = ""
59 //GroupExpiryTime - group expiry time in minutes
60 GroupExpiryTime uint32 = 15
61)
62
63const (
64 // JoinUnsuccessful constant
65 JoinUnsuccessful string = "JOIN-UNSUCCESSFUL"
66 // JoinUnsuccessfulExceededIGMPChanel constant
67 JoinUnsuccessfulExceededIGMPChanel string = "Exceeded subscriber or PON port IGMP channels threshold"
68 // JoinUnsuccessfulAddFlowGroupFailed constant
69 JoinUnsuccessfulAddFlowGroupFailed string = "Failed to add flow or group for a channel"
70 // JoinUnsuccessfulGroupNotConfigured constant
71 JoinUnsuccessfulGroupNotConfigured string = "Join received from a subscriber on non-configured group"
72 // JoinUnsuccessfulVlanDisabled constant
73 JoinUnsuccessfulVlanDisabled string = "Vlan is disabled"
74 // JoinUnsuccessfulDescription constant
75 JoinUnsuccessfulDescription string = "igmp join unsuccessful"
76 // QueryExpired constant
77 QueryExpired string = "QUERY-EXPIRED"
78 // QueryExpiredGroupSpecific constant
79 QueryExpiredGroupSpecific string = "Group specific multicast query expired"
80 // QueryExpiredDescription constant
81 QueryExpiredDescription string = "igmp query expired"
82)
83
Naveen Sampath04696f72022-06-13 15:19:14 +053084// McastConfig structure
85type McastConfig struct {
86 OltSerialNum string
87 MvlanProfileID string
88 IgmpProfileID string
Naveen Sampath04696f72022-06-13 15:19:14 +053089 Version string
90 // This map will help in updating the igds whenever there is a igmp profile id update
91 IgmpGroupDevices sync.Map `json:"-"` // Key is group id
vinokuma926cb3e2023-03-29 11:41:06 +053092 IgmpProxyIP net.IP
93 OperState OperInProgress
Naveen Sampath04696f72022-06-13 15:19:14 +053094}
95
96var (
97 // NullIPAddr is null ip address var
98 NullIPAddr = net.ParseIP("0.0.0.0")
Tinoj Josephcf161be2022-07-07 19:47:47 +053099 // AllSystemsMulticastGroupIP
100 AllSystemsMulticastGroupIP = net.ParseIP("224.0.0.1")
Naveen Sampath04696f72022-06-13 15:19:14 +0530101 // igmpSrcMac for the proxy
102 igmpSrcMac string
103)
104
105func init() {
106 RegisterPacketHandler(IGMP, ProcessIgmpPacket)
107}
108
109// ProcessIgmpPacket : CallBack function registered with application to handle IGMP packetIn
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530110func ProcessIgmpPacket(cntx context.Context, device string, port string, pkt gopacket.Packet) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530111 GetApplication().IgmpPacketInd(device, port, pkt)
112}
113
114func ipv4ToUint(ip net.IP) uint32 {
115 result := uint32(0)
116 addr := ip.To4()
117 if addr == nil {
118 logger.Warnw(ctx, "Invalid Group Addr", log.Fields{"IP": ip})
119 return 0
120 }
121 result = result + uint32(addr[0])<<24
122 result = result + uint32(addr[1])<<16
123 result = result + uint32(addr[2])<<8
124 result = result + uint32(addr[3])
125 return result
126}
127
128func getPodMacAddr() (string, error) {
129 ifas, err := net.Interfaces()
130 if err != nil {
131 return "", err
132 }
133 var ipv4Addr net.IP
134 for _, ifa := range ifas {
135 addrs, err := ifa.Addrs()
136 if err != nil {
137 return "", err
138 }
139 for _, addr := range addrs {
140 if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
141 if ipv4Addr.IsGlobalUnicast() {
142 logger.Infow(ctx, "Igmp Static config", log.Fields{"MacAddr": ifa.HardwareAddr.String(), "ipAddr": ipv4Addr})
143 return ifa.HardwareAddr.String(), nil
144 }
145 }
146 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530147 }
148 return "", errors.New("MAC Address not found,Setting default")
149}
150
151// IgmpUsEthLayer : Layers defined for upstream communication
152// Ethernet layer for upstream communication
153func IgmpUsEthLayer(mcip net.IP) *layers.Ethernet {
154 eth := &layers.Ethernet{}
155 // TODO: Set the source MAC properly and remove hardcoding
156 eth.SrcMAC, _ = net.ParseMAC(igmpSrcMac)
157 eth.DstMAC, _ = net.ParseMAC("01:00:5e:00:00:00")
158 eth.DstMAC[3] = mcip[1] & 0x7f
159 eth.DstMAC[4] = mcip[2]
160 eth.DstMAC[5] = mcip[3]
161 eth.EthernetType = layers.EthernetTypeDot1Q
162 return eth
163}
164
165// IgmpUsDot1qLayer set US VLAN layer
166func IgmpUsDot1qLayer(vlan of.VlanType, priority uint8) *layers.Dot1Q {
167 dot1q := &layers.Dot1Q{}
168 dot1q.Priority = priority
169 dot1q.DropEligible = false
170 dot1q.VLANIdentifier = uint16(vlan)
171 dot1q.Type = layers.EthernetTypeIPv4
172 return dot1q
173}
174
175// Igmpv2UsIpv4Layer : Set the IP layer for IGMPv2
176// TODO - Identify correct way of obtaining source IP
177// This should be the configured IGMP proxy address which should be per OLT
178// We should probably be able to have a single function for both
179// upstream and downstream
180func Igmpv2UsIpv4Layer(src net.IP, mcip net.IP) *layers.IPv4 {
181 ip := &layers.IPv4{}
182 ip.Version = 4
183 ip.Protocol = layers.IPProtocolIGMP
184 ip.TTL = 1
185 ip.SrcIP = src
186 ip.DstIP = mcip
187 return ip
188}
189
190// Igmpv3UsIpv4Layer : Set the IP layer for IGMPv3
191// TODO - Identify correct way of obtaining source IP
192// This should be the configured IGMP proxy address which should be per OLT
193// We should probably be able to have a single function for both
194// upstream and downstream
195func Igmpv3UsIpv4Layer(src net.IP) *layers.IPv4 {
196 ip := &layers.IPv4{}
197 ip.Version = 4
198 ip.Protocol = layers.IPProtocolIGMP
199 ip.TTL = 1
200 ip.SrcIP = src
201 ip.DstIP = net.ParseIP("224.0.0.22")
202 return ip
203}
204
205// IgmpDsEthLayer : Layers defined for downstream communication
206// Ethernet layer for downstream communication
207func IgmpDsEthLayer(mcip net.IP) *layers.Ethernet {
208 eth := &layers.Ethernet{}
209 // TODO: Set the source and dest MAC properly and remove hardcoding
210 eth.SrcMAC, _ = net.ParseMAC(igmpSrcMac)
211 eth.DstMAC, _ = net.ParseMAC("01:00:5e:00:00:00")
212 eth.DstMAC[3] = mcip[1] & 0x7f
213 eth.DstMAC[4] = mcip[2]
214 eth.DstMAC[5] = mcip[3]
215 eth.EthernetType = layers.EthernetTypeDot1Q
216 return eth
217}
218
219// IgmpDsDot1qLayer set the DS VLAN layer
220func IgmpDsDot1qLayer(vlan of.VlanType, priority uint8) *layers.Dot1Q {
221 dot1q := &layers.Dot1Q{}
222 dot1q.Priority = priority
223 dot1q.DropEligible = false
224 dot1q.VLANIdentifier = uint16(vlan)
225 dot1q.Type = layers.EthernetTypeIPv4
226 return dot1q
227}
228
229// IgmpDsIpv4Layer set the IP layer
230func IgmpDsIpv4Layer(src net.IP, mcip net.IP) *layers.IPv4 {
231 ip := &layers.IPv4{}
232 ip.Version = 4
233 ip.Protocol = layers.IPProtocolIGMP
234 ip.TTL = 1
235 ip.SrcIP = src
236 if mcip.Equal(net.ParseIP("0.0.0.0")) {
237 mcip = net.ParseIP("224.0.0.1")
238 }
239 ip.DstIP = mcip
240 return ip
241}
242
243// IgmpQueryv2Layer : IGMP Query Layer
244func IgmpQueryv2Layer(mcip net.IP, resptime time.Duration) *layers.IGMPv1or2 {
245 igmp := &layers.IGMPv1or2{}
246 igmp.Type = layers.IGMPMembershipQuery
247 igmp.GroupAddress = mcip
248 igmp.MaxResponseTime = resptime
249 return igmp
250}
251
252// IgmpQueryv3Layer : IGMP v3 Query Layer
253func IgmpQueryv3Layer(mcip net.IP, resptime time.Duration) *layers.IGMP {
254 igmp := &layers.IGMP{}
255 igmp.Type = layers.IGMPMembershipQuery
256 igmp.GroupAddress = mcip
257 igmp.MaxResponseTime = resptime
258 return igmp
259}
260
261// IgmpReportv2Layer : IGMP Layer
262func IgmpReportv2Layer(mcip net.IP) *layers.IGMPv1or2 {
263 igmp := &layers.IGMPv1or2{}
264 igmp.Type = layers.IGMPMembershipReportV2
265 igmp.GroupAddress = mcip
266 return igmp
267}
268
269// IgmpLeavev2Layer : IGMP Leave Layer
270func IgmpLeavev2Layer(mcip net.IP) *layers.IGMPv1or2 {
271 igmp := &layers.IGMPv1or2{}
272 igmp.Type = layers.IGMPLeaveGroup
273 igmp.GroupAddress = mcip
274 return igmp
275}
276
277// IgmpReportv3Layer : IGMP v3 Report Layer
278func IgmpReportv3Layer(mcip net.IP, incl bool, srclist []net.IP) *layers.IGMP {
279 // IGMP base
280 igmp := &layers.IGMP{}
281 igmp.Type = layers.IGMPMembershipReportV3
282 igmp.NumberOfGroupRecords = 1
283
284 // IGMP Group
285 group := layers.IGMPv3GroupRecord{}
286 if incl {
287 group.Type = layers.IGMPIsIn
288 } else {
289 group.Type = layers.IGMPIsEx
290 }
291 group.MulticastAddress = mcip
292 group.NumberOfSources = uint16(len(srclist))
293 group.SourceAddresses = srclist
294 igmp.GroupRecords = append(igmp.GroupRecords, group)
295
296 return igmp
297}
298
299// Igmpv2QueryPacket : IGMP Query in Downstream
300func Igmpv2QueryPacket(mcip net.IP, vlan of.VlanType, selfip net.IP, pbit uint8, maxResp uint32) ([]byte, error) {
301 // Construct the layers that form the packet
302 eth := IgmpDsEthLayer(mcip)
303 dot1q := IgmpDsDot1qLayer(vlan, pbit)
304 ip := IgmpDsIpv4Layer(selfip, mcip)
305 igmp := IgmpQueryv2Layer(mcip, time.Duration(maxResp)*time.Second)
306
307 // Now prepare the buffer into which the layers are to be serialized
308 buff := gopacket.NewSerializeBuffer()
309 opts := gopacket.SerializeOptions{
310 FixLengths: true,
311 ComputeChecksums: true,
312 }
313 if err := gopacket.SerializeLayers(buff, opts, eth, dot1q, ip, igmp); err != nil {
314 logger.Error(ctx, "Error in serializing layers")
315 return nil, err
316 }
317 return buff.Bytes(), nil
318}
319
320// Igmpv3QueryPacket : IGMPv3 Query in Downstream
321func Igmpv3QueryPacket(mcip net.IP, vlan of.VlanType, selfip net.IP, pbit uint8, maxResp uint32) ([]byte, error) {
322 // Construct the layers that form the packet
323 eth := IgmpDsEthLayer(mcip)
324 dot1q := IgmpDsDot1qLayer(vlan, pbit)
325 ip := IgmpDsIpv4Layer(selfip, mcip)
326 igmp := IgmpQueryv3Layer(mcip, time.Duration(maxResp)*time.Second)
327
328 // Now prepare the buffer into which the layers are to be serialized
329 buff := gopacket.NewSerializeBuffer()
330 opts := gopacket.SerializeOptions{
331 FixLengths: true,
332 ComputeChecksums: true,
333 }
334 if err := gopacket.SerializeLayers(buff, opts, eth, dot1q, ip, igmp); err != nil {
335 logger.Error(ctx, "Error in serializing layers")
336 return nil, err
337 }
338 return buff.Bytes(), nil
339}
340
341// IgmpReportv2Packet : Packet - IGMP v2 report in upstream
342func IgmpReportv2Packet(mcip net.IP, vlan of.VlanType, priority uint8, selfip net.IP) ([]byte, error) {
343 // Construct the layers that form the packet
344 eth := IgmpUsEthLayer(mcip)
345 dot1q := IgmpUsDot1qLayer(vlan, priority)
346 ip := Igmpv2UsIpv4Layer(selfip, mcip)
347 igmp := IgmpReportv2Layer(mcip)
348
349 // Now prepare the buffer into which the layers are to be serialized
350 buff := gopacket.NewSerializeBuffer()
351 opts := gopacket.SerializeOptions{
352 FixLengths: true,
353 ComputeChecksums: true,
354 }
355 if err := gopacket.SerializeLayers(buff, opts, eth, dot1q, ip, igmp); err != nil {
356 logger.Error(ctx, "Error in serializing layers")
357 return nil, err
358 }
359 return buff.Bytes(), nil
360}
361
362// Igmpv3ReportPacket : Packet - IGMP v3 report in upstream
363func Igmpv3ReportPacket(mcip net.IP, vlan of.VlanType, priority uint8, selfip net.IP, incl bool, srclist []net.IP) ([]byte, error) {
364 // Construct the layers that form the packet
365 eth := IgmpUsEthLayer(net.ParseIP("224.0.0.22").To4())
366 dot1q := IgmpUsDot1qLayer(vlan, priority)
367 ip := Igmpv3UsIpv4Layer(selfip)
368 igmp := IgmpReportv3Layer(mcip, incl, srclist)
369
370 // Now prepare the buffer into which the layers are to be serialized
371 buff := gopacket.NewSerializeBuffer()
372 opts := gopacket.SerializeOptions{
373 FixLengths: true,
374 ComputeChecksums: true,
375 }
376 if err := gopacket.SerializeLayers(buff, opts, eth, dot1q, ip, igmp); err != nil {
377 logger.Error(ctx, "Error in serializing layers")
378 return nil, err
379 }
380 return buff.Bytes(), nil
381}
382
383// IgmpLeavePacket : Packet- IGMP Leave in upstream
384func IgmpLeavePacket(mcip net.IP, vlan of.VlanType, priority uint8, selfip net.IP) ([]byte, error) {
385 // Construct the layers that form the packet
386 eth := IgmpUsEthLayer(mcip)
387 dot1q := IgmpUsDot1qLayer(vlan, priority)
388 ip := Igmpv2UsIpv4Layer(selfip, mcip)
389 igmp := IgmpLeavev2Layer(mcip)
390
391 // Now prepare the buffer into which the layers are to be serialized
392 buff := gopacket.NewSerializeBuffer()
393 opts := gopacket.SerializeOptions{
394 FixLengths: true,
395 ComputeChecksums: true,
396 }
397 if err := gopacket.SerializeLayers(buff, opts, eth, dot1q, ip, igmp); err != nil {
398 logger.Error(ctx, "Error in serializing layers")
399 return nil, err
400 }
401 return buff.Bytes(), nil
402}
403
404// getVersion to get igmp version type
405func getVersion(ver string) uint8 {
406 if ver == "2" || ver == "v2" {
407 return IgmpVersion2
408 }
409 return IgmpVersion3
410}
411
412// IsIPPresent is Utility to check if an IP address is in a list
413func IsIPPresent(i net.IP, ips []net.IP) bool {
414 for _, ip := range ips {
415 if i.Equal(ip) {
416 return true
417 }
418 }
419 return false
420}
421
vinokuma926cb3e2023-03-29 11:41:06 +0530422// AddToPendingPool - adds Igmp Device obj to pending pool
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530423func AddToPendingPool(cntx context.Context, device string, groupKey string) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530424 logger.Infow(ctx, "Add Device to IgmpGroup Pending Pool", log.Fields{"Device": device, "GroupKey": groupKey})
425 if grp, ok := GetApplication().IgmpGroups.Load(groupKey); ok {
426 ig := grp.(*IgmpGroup)
427 ig.PendingPoolLock.Lock()
428 logger.Infow(ctx, "Adding Device to IgmpGroup Pending Pool", log.Fields{"Device": device, "GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr.String()})
429 ig.PendingGroupForDevice[device] = time.Now().Add(time.Duration(GroupExpiryTime) * time.Minute)
430 ig.PendingPoolLock.Unlock()
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530431 if err := ig.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530432 logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName})
433 }
434 return true
435 }
436 return false
437}
438
439/*
440func checkIfForceGroupRemove(device string) bool {
441 if d := GetApplication().GetDevice(device); d != nil {
442 if d.State == cntlr.DeviceStateREBOOTED || d.State == cntlr.DeviceStateDOWN {
443 return true
444 }
445 }
446 return false
447}*/
448
Naveen Sampath04696f72022-06-13 15:19:14 +0530449// SendQueryExpiredEventGroupSpecific to send group specific query expired event.
450func SendQueryExpiredEventGroupSpecific(portKey string, igd *IgmpGroupDevice, igc *IgmpGroupChannel) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530451 logger.Info(ctx, "Processing-SendQueryExpiredEventGroupSpecific-Event")
452 va := GetApplication()
453 mvpName := va.GetMvlanProfileByTag(igd.Mvlan).Name
454
455 sendEvent := func(key interface{}, value interface{}) bool {
456 if value.(*VoltService).IgmpEnabled && value.(*VoltService).MvlanProfileName == mvpName {
457 logger.Debugw(ctx, "sending-query-expired-group-specific-event", log.Fields{"EventType": QueryExpiredGroupSpecific, "ServiceName": value.(*VoltService).Name})
458 }
459 return false
460 }
461
462 // Fetching service name to send with query expired event.
463 vpvs, _ := va.VnetsByPort.Load(portKey)
464 if vpvs == nil {
465 logger.Errorw(ctx, "volt-port-vnet-is-nil", log.Fields{"vpvs": vpvs})
466 return
467 }
468
469 for _, vpv := range vpvs.([]*VoltPortVnet) {
470 vpv.services.Range(sendEvent)
471 }
472}
473
474// GetMcastServiceForSubAlarm to get mcast service name for subscriber alarm.
475func GetMcastServiceForSubAlarm(uniPort *VoltPort, mvp *MvlanProfile) string {
Naveen Sampath04696f72022-06-13 15:19:14 +0530476 var serviceName string
477 mvpName := mvp.Name
478
479 va := GetApplication()
480
481 sendAlm := func(key interface{}, value interface{}) bool {
482 if value.(*VoltService).IgmpEnabled && value.(*VoltService).MvlanProfileName == mvpName {
483 serviceName = value.(*VoltService).Name
484 }
485 return true
486 }
487
488 // Fetching service name to send with active channels exceeded per subscriber alarm.
489 vpvs, _ := va.VnetsByPort.Load(uniPort.Name)
490 if vpvs == nil {
491 logger.Errorw(ctx, "volt-port-vnet-is-nil", log.Fields{"vpvs": vpvs})
492 return serviceName
493 }
494
495 for _, vpv := range vpvs.([]*VoltPortVnet) {
496 vpv.services.Range(sendAlm)
497 }
498
499 return serviceName
Naveen Sampath04696f72022-06-13 15:19:14 +0530500}
501
Naveen Sampath04696f72022-06-13 15:19:14 +0530502// RestoreIgmpGroupsFromDb to restore igmp groups from database
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530503func (va *VoltApplication) RestoreIgmpGroupsFromDb(cntx context.Context) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530504 groups, _ := db.GetIgmpGroups(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530505 for _, group := range groups {
506 b, ok := group.Value.([]byte)
507 if !ok {
508 logger.Warn(ctx, "The value type is not []byte")
509 continue
510 }
511 var ig IgmpGroup
512 err := json.Unmarshal(b, &ig)
513 if err != nil {
514 logger.Warn(ctx, "Unmarshal of IGMP Group failed")
515 continue
516 }
517 ig.Devices = make(map[string]*IgmpGroupDevice)
518
519 //For Upgrade Case
520 if len(ig.PendingGroupForDevice) == 0 {
521 ig.PendingGroupForDevice = make(map[string]time.Time)
522 }
523 logger.Infow(ctx, "Restoring Groups", log.Fields{"igGroupID": ig.GroupID, "igGroupName": ig.GroupName, "igMvlan": ig.Mvlan})
524 grpKey := ig.getKey()
525 va.IgmpGroups.Store(grpKey, &ig)
526 // Just delete and lose the IGMP group with the same group Id
527 if _, err := va.GetIgmpGroupID(ig.GroupID); err != nil {
528 logger.Warnw(ctx, "GetIgmpGroupID Failed", log.Fields{"igGroupID": ig.GroupID, "Error": err})
529 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530530 ig.RestoreDevices(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +0530531
532 if ig.NumDevicesActive() == 0 {
533 va.AddGroupToPendingPool(&ig)
534 }
535 logger.Infow(ctx, "Restored Groups", log.Fields{"igGroupID": ig.GroupID, "igGroupName": ig.GroupName, "igMvlan": ig.Mvlan})
536 }
537}
538
539// AddIgmpGroup : When the first IGMP packet is received, the MVLAN profile is identified
540// for the IGMP group and grp obj is obtained from the available pending pool of groups.
541// If not, new group obj will be created based on available group IDs
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530542func (va *VoltApplication) AddIgmpGroup(cntx context.Context, mvpName string, gip net.IP, device string) *IgmpGroup {
Naveen Sampath04696f72022-06-13 15:19:14 +0530543 var ig *IgmpGroup
544 if mvp, grpName := va.GetMvlanProfileForMcIP(mvpName, gip); mvp != nil {
545 if ig = va.GetGroupFromPendingPool(mvp.Mvlan, device); ig != nil {
546 logger.Infow(ctx, "Igmp Group obtained from global pending pool", log.Fields{"MvlanProfile": mvpName, "GroupID": ig.GroupID, "Device": device, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr.String()})
547 oldKey := mvp.generateGroupKey(ig.GroupName, ig.GroupAddr.String())
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530548 ig.IgmpGroupReInit(cntx, grpName, gip)
Naveen Sampath04696f72022-06-13 15:19:14 +0530549 ig.IsGroupStatic = mvp.Groups[grpName].IsStatic
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530550 ig.UpdateIgmpGroup(cntx, oldKey, ig.getKey())
Naveen Sampath04696f72022-06-13 15:19:14 +0530551 } else {
552 logger.Infow(ctx, "No Igmp Group available in global pending pool. Creating new Igmp Group", log.Fields{"MvlanProfile": mvpName, "Device": device, "GroupAddr": gip.String()})
553 if ig = va.GetAvailIgmpGroupID(); ig == nil {
554 logger.Error(ctx, "Igmp Group Creation Failed: Group Id Unavailable")
555 return nil
556 }
557 ig.IgmpGroupInit(grpName, gip, mvp)
558 grpKey := ig.getKey()
559 va.IgmpGroups.Store(grpKey, ig)
560 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530561 if err := ig.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530562 logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName})
563 }
564 return ig
565 }
Tinoj Joseph1d108322022-07-13 10:07:39 +0530566 logger.Errorw(ctx, "GetMvlan Pro failed", log.Fields{"Group": gip})
Naveen Sampath04696f72022-06-13 15:19:14 +0530567 return nil
568}
569
570// GetIgmpGroup helps search for the IGMP group from the list of
571// active IGMP groups. For now, the assumption is that a group
572// cannot belong to more than on MVLAN. If we change that definition,
573// we have to take a relook at this implementation. The key will include
574// both MVLAN and the group IP.
575func (va *VoltApplication) GetIgmpGroup(mvlan of.VlanType, gip net.IP) *IgmpGroup {
Naveen Sampath04696f72022-06-13 15:19:14 +0530576 profile, _ := va.MvlanProfilesByTag.Load(mvlan)
577 if profile == nil {
578 logger.Errorw(ctx, "Mvlan Profile not found for incoming packet. Dropping Request", log.Fields{"Mvlan": mvlan, "GroupAddr": gip.String()})
579 return nil
580 }
581 mvp := profile.(*MvlanProfile)
582 _, gName := va.GetMvlanProfileForMcIP(mvp.Name, gip)
583 grpKey := mvp.generateGroupKey(gName, gip.String())
584 logger.Debugw(ctx, "Get IGMP Group", log.Fields{"Group": grpKey})
585 igIntf, ok := va.IgmpGroups.Load(grpKey)
586 if ok {
587 logger.Debugw(ctx, "Get IGMP Group Success", log.Fields{"Group": grpKey})
588 ig := igIntf.(*IgmpGroup)
589
590 //Case: Group was part of pending and Join came with same channel or different channel from same group
591 // (from same or different device)
592 // In that case, the same group will be allocated since the group is still part of va.IgmpGroups
593 // So, the groups needs to be removed from global pending pool
594 va.RemoveGroupDevicesFromPendingPool(ig)
595 return ig
596 }
597 return nil
598}
599
Naveen Sampath04696f72022-06-13 15:19:14 +0530600// DelIgmpGroup : When the last subscriber leaves the IGMP group across all the devices
601// the IGMP group is removed.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530602func (va *VoltApplication) DelIgmpGroup(cntx context.Context, ig *IgmpGroup) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530603 profile, found := GetApplication().MvlanProfilesByTag.Load(ig.Mvlan)
604 if found {
605 mvp := profile.(*MvlanProfile)
606
607 grpKey := mvp.generateGroupKey(ig.GroupName, ig.GroupAddr.String())
608
609 if igIntf, ok := va.IgmpGroups.Load(grpKey); ok {
610 ig := igIntf.(*IgmpGroup)
611 ig.IgmpGroupLock.Lock()
612 if ig.NumDevicesAll() == 0 {
613 logger.Debugw(ctx, "Deleting IGMP Group", log.Fields{"Group": grpKey})
614 va.PutIgmpGroupID(ig)
615 va.IgmpGroups.Delete(grpKey)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530616 _ = db.DelIgmpGroup(cntx, grpKey)
Naveen Sampath04696f72022-06-13 15:19:14 +0530617 } else {
618 logger.Infow(ctx, "Skipping IgmpGroup Device. Pending Igmp Group Devices present", log.Fields{"GroupID": ig.GroupID, "GroupName": ig.GroupName, "GroupAddr": ig.GroupAddr.String(), "PendingDevices": len(ig.Devices)})
619 va.AddGroupToPendingPool(ig)
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530620 if err := ig.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530621 logger.Errorw(ctx, "Igmp group Write to DB failed", log.Fields{"groupName": ig.GroupName})
622 }
623 }
624 ig.IgmpGroupLock.Unlock()
625 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530626 }
627}
628
629// GetPonPortID Gets the PON port ID from uniPortID
630func (va *VoltApplication) GetPonPortID(device, uniPortID string) uint32 {
Naveen Sampath04696f72022-06-13 15:19:14 +0530631 isNNI := strings.Contains(uniPortID, "nni")
632 if isNNI || uniPortID == StaticPort {
633 logger.Debugw(ctx, "Cannot get pon port from UNI port", log.Fields{"port": uniPortID})
634 return 0xFF
635 }
636 dIntf, ok := va.DevicesDisc.Load(device)
637 if !ok {
638 return 0xFF
639 }
640 d := dIntf.(*VoltDevice)
641
642 uniPort := d.GetPort(uniPortID)
643 if uniPort == nil {
644 return 0xFF
645 }
646 return GetPonPortIDFromUNIPort(uniPort.ID)
647}
648
649// AggActiveChannelsCountPerSub aggregates the active channel count for given uni port.
650// It will iterate over all the groups and store the sum of active channels in VoltPort.
651func (va *VoltApplication) AggActiveChannelsCountPerSub(device, uniPort string, port *VoltPort) {
652 var activeChannelCount uint32
653
654 collectActiveChannelCount := func(key interface{}, value interface{}) bool {
655 ig := value.(*IgmpGroup)
656 igd := ig.Devices[device]
657 if igd == nil {
658 return true
659 }
660 if portChannels, ok := igd.PortChannelMap.Load(uniPort); ok {
661 channelList := portChannels.([]net.IP)
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530662 channelLength := len(channelList)
663 // Check if the length exceeds uint32's maximum value
664 if channelLength > int(^uint32(0)) {
665 logger.Error(ctx, "Error converting string to uint32")
666 }
667 activeChannelCount += uint32(channelLength)
Naveen Sampath04696f72022-06-13 15:19:14 +0530668 }
669 return true
670 }
671 va.IgmpGroups.Range(collectActiveChannelCount)
672
673 logger.Debugw(ctx, "AggrActiveChannelCount for Subscriber",
674 log.Fields{"UniPortID": uniPort, "count": activeChannelCount})
675
676 port.ActiveChannels = activeChannelCount
677}
678
679// AggActiveChannelsCountForPonPort Aggregates the active channel count for given pon port.
680// It will iterate over all the groups and store the sum of active channels in VoltDevice.
681func (va *VoltApplication) AggActiveChannelsCountForPonPort(device string, ponPortID uint32, port *PonPortCfg) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530682 var activeChannelCount uint32
683
684 collectActiveChannelCount := func(key interface{}, value interface{}) bool {
685 ig := value.(*IgmpGroup)
686 igd := ig.Devices[device]
687 if igd == nil {
688 return true
689 }
690 if ponPortChannels, ok := igd.PonPortChannelMap.Get(ponPortID); ok {
691 activeChannelCount += ponPortChannels.(*PonPortChannels).GetActiveChannelCount()
692 }
693 return true
694 }
695 va.IgmpGroups.Range(collectActiveChannelCount)
696
697 logger.Debugw(ctx, "AggrActiveChannelCount for Pon Port",
698 log.Fields{"PonPortID": ponPortID, "count": activeChannelCount})
699
700 port.ActiveIGMPChannels = activeChannelCount
701}
702
703// UpdateActiveChannelCountForPonPort increments the global counter for active
704// channel count per pon port.
705func (va *VoltApplication) UpdateActiveChannelCountForPonPort(device, uniPortID string, ponPortID uint32, isAdd, isChannel bool, igd *IgmpGroupDevice) {
706 incrDecr := func(value uint32) uint32 {
707 if isAdd {
708 return value + 1
709 }
710 return value - 1
711 }
712 if d, exists := va.DevicesDisc.Load(device); exists {
713 voltDevice := d.(*VoltDevice)
714
715 if isChannel {
716 voltDevice.ActiveChannelCountLock.Lock()
717 // If New channel is added/deleted, then only update the ActiveChannelsPerPon
718 if value, ok := voltDevice.ActiveChannelsPerPon.Load(ponPortID); ok {
719 port := value.(*PonPortCfg)
720 port.ActiveIGMPChannels = incrDecr(port.ActiveIGMPChannels)
721 voltDevice.ActiveChannelsPerPon.Store(ponPortID, port)
722 logger.Debugw(ctx, "+++ActiveChannelsPerPon", log.Fields{"count": port.ActiveIGMPChannels}) // TODO: remove me
723 }
724 voltDevice.ActiveChannelCountLock.Unlock()
725 }
726 if uPort, ok := voltDevice.Ports.Load(uniPortID); ok {
727 uniPort := uPort.(*VoltPort)
728 uniPort.ActiveChannels = incrDecr(uniPort.ActiveChannels)
729 voltDevice.Ports.Store(uniPortID, uniPort)
730 logger.Debugw(ctx, "+++ActiveChannelsPerSub", log.Fields{"count": uniPort.ActiveChannels}) // TODO: remove me
731 }
732 }
733}
734
735// IsMaxChannelsCountExceeded checks if the PON port active channel
736// capacity and subscriber level channel capacity is reached to max allowed
737// channel per pon threshold. If Exceeds, return true else return false.
738func (va *VoltApplication) IsMaxChannelsCountExceeded(device, uniPortID string,
739 ponPortID uint32, ig *IgmpGroup, channelIP net.IP, mvp *MvlanProfile) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530740 // New receiver check is required to identify the IgmpReportMsg received
741 // in response to the IGMP Query sent from VGC.
742 if newReceiver := ig.IsNewReceiver(device, uniPortID, channelIP); !newReceiver {
743 logger.Debugw(ctx, "Not a new receiver. It is a response to IGMP Query",
744 log.Fields{"port": uniPortID, "channel": channelIP})
745 return false
746 }
747
748 if vDev, exists := va.DevicesDisc.Load(device); exists {
749 voltDevice := vDev.(*VoltDevice)
750
751 // Checking subscriber active channel count with maxChannelsAllowedPerSub
752 if uniPort, present := voltDevice.Ports.Load(uniPortID); present {
753 if uniPort.(*VoltPort).ActiveChannels >= mvp.MaxActiveChannels {
754 logger.Errorw(ctx, "Max allowed channels per subscriber is exceeded",
755 log.Fields{"activeCount": uniPort.(*VoltPort).ActiveChannels, "channel": channelIP, "UNI": uniPort.(*VoltPort).Name})
756 if !(uniPort.(*VoltPort).ChannelPerSubAlarmRaised) {
757 serviceName := GetMcastServiceForSubAlarm(uniPort.(*VoltPort), mvp)
758 logger.Debugw(ctx, "Raising-SendActiveChannelPerSubscriberAlarm-Initiated", log.Fields{"ActiveChannels": uniPort.(*VoltPort).ActiveChannels, "ServiceName": serviceName})
759 uniPort.(*VoltPort).ChannelPerSubAlarmRaised = true
760 }
761 return true
762 }
763 } else {
764 logger.Errorw(ctx, "UNI port not found in VoltDevice", log.Fields{"uniPortID": uniPortID})
765 }
766 if value, ok := voltDevice.ActiveChannelsPerPon.Load(ponPortID); ok {
767 ponPort := value.(*PonPortCfg)
768
769 logger.Debugw(ctx, "----Active channels count for PON port",
770 log.Fields{"PonPortID": ponPortID, "activeChannels": ponPort.ActiveIGMPChannels,
771 "maxAllowedChannelsPerPon": ponPort.MaxActiveChannels})
772
773 if ponPort.ActiveIGMPChannels < ponPort.MaxActiveChannels {
774 // PON port active channel capacity is not yet reached to max allowed channels per pon.
775 // So allowing to add receiver.
776 return false
777 } else if ponPort.ActiveIGMPChannels >= ponPort.MaxActiveChannels && ig != nil {
778 // PON port active channel capacity is reached to max allowed channels per pon.
779 // Check if same channel is already configured on that PON port.
780 // If that channel is present, then allow AddReceiver else it will be rejected.
781 igd, isPresent := ig.Devices[device]
782 if isPresent {
783 if channelListForPonPort, _ := igd.PonPortChannelMap.Get(ponPortID); channelListForPonPort != nil {
784 if _, isExists := channelListForPonPort.(*PonPortChannels).ChannelList.Get(channelIP.String()); isExists {
785 return false
786 }
787 }
788 }
789 }
790 logger.Errorw(ctx, "Active channels count for PON port exceeded",
791 log.Fields{"PonPortID": ponPortID, "activeChannels": ponPort.ActiveIGMPChannels, "channel": channelIP, "UNI": uniPortID})
792 } else {
793 logger.Warnw(ctx, "PON port level active channel count does not exists",
794 log.Fields{"ponPortID": ponPortID})
795 return false
796 }
797 }
798 logger.Warnw(ctx, "Max allowed channels per pon threshold is reached", log.Fields{"PonPortID": ponPortID})
799 return true
800}
801
802// ProcessIgmpv2Pkt : This is IGMPv2 packet.
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530803func (va *VoltApplication) ProcessIgmpv2Pkt(cntx context.Context, device string, port string, pkt gopacket.Packet) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530804 // First get the layers of interest
805 dot1Q := pkt.Layer(layers.LayerTypeDot1Q).(*layers.Dot1Q)
806 pktVlan := of.VlanType(dot1Q.VLANIdentifier)
807 igmpv2 := pkt.Layer(layers.LayerTypeIGMP).(*layers.IGMPv1or2)
808
809 ponPortID := va.GetPonPortID(device, port)
810
811 var vpv *VoltPortVnet
812
813 logger.Debugw(ctx, "Received IGMPv2 Type", log.Fields{"Type": igmpv2.Type})
814
815 if igmpv2.Type == layers.IGMPMembershipReportV2 || igmpv2.Type == layers.IGMPMembershipReportV1 {
Naveen Sampath04696f72022-06-13 15:19:14 +0530816 logger.Infow(ctx, "IGMP Join received: v2", log.Fields{"Addr": igmpv2.GroupAddress, "Port": port})
817
818 // This is a report coming from the PON. We must be able to first find the
819 // subscriber from the VLAN tag and port and verify if the IGMP proxy is
820 // enabled for the subscriber
821 vpv, _ = va.GetVnetFromPkt(device, port, pkt)
822
823 if vpv == nil {
824 logger.Errorw(ctx, "Couldn't find VNET associated with port", log.Fields{"Port": port})
825 return
826 } else if !vpv.IgmpEnabled {
827 logger.Errorw(ctx, "IGMP is not activated on the port", log.Fields{"Port": port})
828 return
829 }
830
831 mvp := va.GetMvlanProfileByName(vpv.MvlanProfileName)
832 if mvp == nil {
833 logger.Errorw(ctx, "Igmp Packet Received for Subscriber with Missing Mvlan Profile",
834 log.Fields{"Receiver": vpv.Port, "MvlanProfile": vpv.MvlanProfileName})
835 return
836 }
837 mvlan := mvp.Mvlan
838
839 mvp.mvpLock.RLock()
840 defer mvp.mvpLock.RUnlock()
841 // The subscriber is validated and now process the IGMP report
842 ig := va.GetIgmpGroup(mvlan, igmpv2.GroupAddress)
843
844 if yes := va.IsMaxChannelsCountExceeded(device, port, ponPortID, ig, igmpv2.GroupAddress, mvp); yes {
845 logger.Warnw(ctx, "Dropping IGMP Join v2: Active channel threshold exceeded",
846 log.Fields{"PonPortID": ponPortID, "Addr": igmpv2.GroupAddress, "MvlanProfile": vpv.MvlanProfileName})
847 return
848 }
849 if ig != nil {
850 logger.Infow(ctx, "IGMP Group", log.Fields{"Group": ig.GroupID, "devices": ig.Devices})
851 // If the IGMP group is already created. just add the receiver
852 ig.IgmpGroupLock.Lock()
853 // Check for port state to avoid race condition where PortDown event
854 // acquired lock before packet processing
855 vd := GetApplication().GetDevice(device)
856 vp := vd.GetPort(port)
857 if vp == nil || vp.State != PortStateUp {
858 logger.Warnw(ctx, "Join received from a Port that is DOWN or not present",
Akash Sonia8246972023-01-03 10:37:08 +0530859 log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530860 ig.IgmpGroupLock.Unlock()
861 return
862 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530863 ig.AddReceiver(cntx, device, port, igmpv2.GroupAddress, nil, IgmpVersion2, dot1Q.VLANIdentifier, dot1Q.Priority, ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530864 ig.IgmpGroupLock.Unlock()
865 } else {
866 // Create the IGMP group and then add the receiver to the group
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530867 if ig := va.AddIgmpGroup(cntx, vpv.MvlanProfileName, igmpv2.GroupAddress, device); ig != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530868 logger.Infow(ctx, "New IGMP Group", log.Fields{"Group": ig.GroupID, "devices": ig.Devices})
869 ig.IgmpGroupLock.Lock()
870 // Check for port state to avoid race condition where PortDown event
871 // acquired lock before packet processing
872 vd := GetApplication().GetDevice(device)
873 vp := vd.GetPort(port)
874 if vp == nil || vp.State != PortStateUp {
875 logger.Warnw(ctx, "Join received from a Port that is DOWN or not present",
Akash Sonia8246972023-01-03 10:37:08 +0530876 log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530877 ig.IgmpGroupLock.Unlock()
878 return
879 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530880 ig.AddReceiver(cntx, device, port, igmpv2.GroupAddress, nil, IgmpVersion2, dot1Q.VLANIdentifier, dot1Q.Priority, ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530881 ig.IgmpGroupLock.Unlock()
882 } else {
883 logger.Errorw(ctx, "IGMP Group Creation Failed", log.Fields{"Addr": igmpv2.GroupAddress})
884 return
885 }
886 }
887 } else if igmpv2.Type == layers.IGMPLeaveGroup {
888 // This is a IGMP leave coming from one of the receivers. We essentially remove the
889 // the receiver.
890 logger.Infow(ctx, "IGMP Leave received: v2", log.Fields{"Addr": igmpv2.GroupAddress, "Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +0530891 vpv, _ = va.GetVnetFromPkt(device, port, pkt)
892 if vpv == nil {
893 logger.Errorw(ctx, "Couldn't find VNET associated with port", log.Fields{"Port": port})
894 return
895 } else if !vpv.IgmpEnabled {
896 logger.Errorw(ctx, "IGMP is not activated on the port", log.Fields{"Port": port})
897 return
898 }
899
900 mvp := va.GetMvlanProfileByName(vpv.MvlanProfileName)
901 mvp.mvpLock.RLock()
902 defer mvp.mvpLock.RUnlock()
903 mvlan := mvp.Mvlan
904 // The subscriber is validated and now process the IGMP report
905 if ig := va.GetIgmpGroup(mvlan, igmpv2.GroupAddress); ig != nil {
906 ig.IgmpGroupLock.Lock()
907 // Delete the receiver once the IgmpGroup is identified
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530908 ig.DelReceiver(cntx, device, port, igmpv2.GroupAddress, nil, ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +0530909 ig.IgmpGroupLock.Unlock()
910 if ig.NumDevicesActive() == 0 {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530911 va.DelIgmpGroup(cntx, ig)
Naveen Sampath04696f72022-06-13 15:19:14 +0530912 }
913 }
914 } else {
915 // This must be a query on the NNI port. However, we dont make that assumption.
916 // Need to look for the IGMP group based on the VLAN in the packet as
917 // the MVLAN
918
919 //Check if mvlan profile exist for the incoming pkt vlan
920 profile, _ := va.MvlanProfilesByTag.Load(pktVlan)
921 if profile == nil {
922 logger.Errorw(ctx, "Mvlan Profile not found for incoming packet. Dropping Request", log.Fields{"Mvlan": pktVlan})
923 return
924 }
925 mvp := profile.(*MvlanProfile)
926 mvp.mvpLock.RLock()
927 defer mvp.mvpLock.RUnlock()
928
929 if net.ParseIP("0.0.0.0").Equal(igmpv2.GroupAddress) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530930 va.processIgmpQueries(cntx, device, pktVlan, IgmpVersion2)
Naveen Sampath04696f72022-06-13 15:19:14 +0530931 } else {
932 if ig := va.GetIgmpGroup(pktVlan, igmpv2.GroupAddress); ig != nil {
933 ig.IgmpGroupLock.Lock()
934 igd, ok := ig.Devices[device]
935 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530936 igd.ProcessQuery(cntx, igmpv2.GroupAddress, IgmpVersion2)
Naveen Sampath04696f72022-06-13 15:19:14 +0530937 } else {
938 logger.Warnw(ctx, "IGMP Device not found", log.Fields{"Device": device, "Group": igmpv2.GroupAddress})
939 }
940 ig.IgmpGroupLock.Unlock()
941 }
942 }
943 }
944}
945
946// ProcessIgmpv3Pkt : Process IGMPv3 packet
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530947func (va *VoltApplication) ProcessIgmpv3Pkt(cntx context.Context, device string, port string, pkt gopacket.Packet) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530948 // First get the layers of interest
949 dot1QLayer := pkt.Layer(layers.LayerTypeDot1Q)
950
951 if dot1QLayer == nil {
952 logger.Error(ctx, "Igmp Packet Received without Vlan - Dropping pkt")
953 return
954 }
955 dot1Q := dot1QLayer.(*layers.Dot1Q)
956 pktVlan := of.VlanType(dot1Q.VLANIdentifier)
957 igmpv3 := pkt.Layer(layers.LayerTypeIGMP).(*layers.IGMP)
958
959 ponPortID := va.GetPonPortID(device, port)
960
961 var vpv *VoltPortVnet
962 logger.Debugw(ctx, "Received IGMPv3 Type", log.Fields{"Type": igmpv3.Type})
963
964 if igmpv3.Type == layers.IGMPMembershipReportV3 {
965 // This is a report coming from the PON. We must be able to first find the
966 // subscriber from the VLAN tag and port and verify if the IGMP proxy is
967 // enabled for the subscriber
968 vpv, _ = va.GetVnetFromPkt(device, port, pkt)
969 if vpv == nil {
970 logger.Errorw(ctx, "Couldn't find VNET associated with port", log.Fields{"Port": port})
971 return
972 } else if !vpv.IgmpEnabled {
973 logger.Errorw(ctx, "IGMP is not activated on the port", log.Fields{"Port": port})
974 return
975 }
976 mvp := va.GetMvlanProfileByName(vpv.MvlanProfileName)
977 if mvp == nil {
978 logger.Errorw(ctx, "Igmp Packet received for Subscriber with Missing Mvlan Profile",
979 log.Fields{"Receiver": vpv.Port, "MvlanProfile": vpv.MvlanProfileName})
980 return
981 }
982 mvp.mvpLock.RLock()
983 defer mvp.mvpLock.RUnlock()
984 mvlan := mvp.Mvlan
985
vinokuma926cb3e2023-03-29 11:41:06 +0530986 for i, group := range igmpv3.GroupRecords {
Naveen Sampath04696f72022-06-13 15:19:14 +0530987 isJoin := isIgmpJoin(group.Type, group.SourceAddresses)
988 // The subscriber is validated and now process the IGMP report
989 ig := va.GetIgmpGroup(mvlan, group.MulticastAddress)
990 if isJoin {
991 if yes := va.IsMaxChannelsCountExceeded(device, port, ponPortID, ig, group.MulticastAddress, mvp); yes {
992 logger.Warnw(ctx, "Dropping IGMP Join v3: Active channel threshold exceeded",
993 log.Fields{"PonPortID": ponPortID, "Addr": group.MulticastAddress, "MvlanProfile": vpv.MvlanProfileName})
994
995 return
996 }
997 if ig != nil {
998 // If the IGMP group is already created. just add the receiver
999 logger.Infow(ctx, "IGMP Join received for existing group", log.Fields{"Addr": group.MulticastAddress, "Port": port})
1000 ig.IgmpGroupLock.Lock()
1001 // Check for port state to avoid race condition where PortDown event
1002 // acquired lock before packet processing
1003 vd := GetApplication().GetDevice(device)
1004 vp := vd.GetPort(port)
1005 if vp == nil || vp.State != PortStateUp {
1006 logger.Warnw(ctx, "Join received from a Port that is DOWN or not present",
Akash Sonia8246972023-01-03 10:37:08 +05301007 log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301008 ig.IgmpGroupLock.Unlock()
1009 return
1010 }
vinokuma926cb3e2023-03-29 11:41:06 +05301011 ig.AddReceiver(cntx, device, port, group.MulticastAddress, &igmpv3.GroupRecords[i], IgmpVersion3,
Naveen Sampath04696f72022-06-13 15:19:14 +05301012 dot1Q.VLANIdentifier, dot1Q.Priority, ponPortID)
1013 ig.IgmpGroupLock.Unlock()
1014 } else {
1015 // Create the IGMP group and then add the receiver to the group
1016 logger.Infow(ctx, "IGMP Join received for new group", log.Fields{"Addr": group.MulticastAddress, "Port": port})
vinokuma926cb3e2023-03-29 11:41:06 +05301017 if ig = va.AddIgmpGroup(cntx, vpv.MvlanProfileName, group.MulticastAddress, device); ig != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301018 ig.IgmpGroupLock.Lock()
1019 // Check for port state to avoid race condition where PortDown event
1020 // acquired lock before packet processing
1021 vd := GetApplication().GetDevice(device)
1022 vp := vd.GetPort(port)
1023 if vp == nil || vp.State != PortStateUp {
1024 logger.Warnw(ctx, "Join received from a Port that is DOWN or not present",
Akash Sonia8246972023-01-03 10:37:08 +05301025 log.Fields{"Port": port})
Naveen Sampath04696f72022-06-13 15:19:14 +05301026 ig.IgmpGroupLock.Unlock()
1027 return
1028 }
vinokuma926cb3e2023-03-29 11:41:06 +05301029 ig.AddReceiver(cntx, device, port, group.MulticastAddress, &igmpv3.GroupRecords[i], IgmpVersion3,
Naveen Sampath04696f72022-06-13 15:19:14 +05301030 dot1Q.VLANIdentifier, dot1Q.Priority, ponPortID)
1031 ig.IgmpGroupLock.Unlock()
1032 } else {
1033 logger.Warnw(ctx, "IGMP Group Creation Failed", log.Fields{"Addr": group.MulticastAddress})
1034 }
1035 }
1036 } else if ig != nil {
1037 logger.Infow(ctx, "IGMP Leave received for existing group", log.Fields{"Addr": group.MulticastAddress, "Port": port})
1038 ig.IgmpGroupLock.Lock()
vinokuma926cb3e2023-03-29 11:41:06 +05301039 ig.DelReceiver(cntx, device, port, group.MulticastAddress, &igmpv3.GroupRecords[i], ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301040 ig.IgmpGroupLock.Unlock()
1041 if ig.NumDevicesActive() == 0 {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301042 va.DelIgmpGroup(cntx, ig)
Naveen Sampath04696f72022-06-13 15:19:14 +05301043 }
1044 } else {
1045 logger.Warnw(ctx, "IGMP Leave received for unknown group", log.Fields{"Addr": group.MulticastAddress})
1046 }
1047 }
1048 } else {
1049 // This must be a query on the NNI port. However, we dont make that assumption.
1050 // Need to look for the IGMP group based on the VLAN in the packet as
1051 // the MVLAN
1052
1053 //Check if mvlan profile exist for the incoming pkt vlan
1054 profile, _ := va.MvlanProfilesByTag.Load(pktVlan)
1055 if profile == nil {
1056 logger.Errorw(ctx, "Mvlan Profile not found for incoming packet. Dropping Request", log.Fields{"Mvlan": pktVlan})
1057 return
1058 }
1059 mvp := profile.(*MvlanProfile)
1060 mvp.mvpLock.RLock()
1061 defer mvp.mvpLock.RUnlock()
1062
1063 if net.ParseIP("0.0.0.0").Equal(igmpv3.GroupAddress) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301064 va.processIgmpQueries(cntx, device, pktVlan, IgmpVersion3)
Naveen Sampath04696f72022-06-13 15:19:14 +05301065 } else {
1066 if ig := va.GetIgmpGroup(pktVlan, igmpv3.GroupAddress); ig != nil {
1067 ig.IgmpGroupLock.Lock()
1068 igd, ok := ig.Devices[device]
1069 if ok {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301070 igd.ProcessQuery(cntx, igmpv3.GroupAddress, IgmpVersion3)
Naveen Sampath04696f72022-06-13 15:19:14 +05301071 } else {
1072 logger.Warnw(ctx, "IGMP Device not found", log.Fields{"Device": device, "Group": igmpv3.GroupAddress})
1073 }
1074 ig.IgmpGroupLock.Unlock()
1075 }
1076 }
1077 }
1078}
1079
1080// processIgmpQueries to process the igmp queries
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301081func (va *VoltApplication) processIgmpQueries(cntx context.Context, device string, pktVlan of.VlanType, version uint8) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301082 // This is a generic query and respond with all the groups channels in currently being viewed.
1083 processquery := func(key interface{}, value interface{}) bool {
1084 ig := value.(*IgmpGroup)
1085 ig.IgmpGroupLock.Lock()
1086 if ig.Mvlan != pktVlan {
1087 ig.IgmpGroupLock.Unlock()
1088 return true
1089 }
1090 igd, ok := ig.Devices[device]
1091 if !ok {
1092 logger.Warnw(ctx, "IGMP Device not found", log.Fields{"Device": device})
1093 ig.IgmpGroupLock.Unlock()
1094 return true
1095 }
1096 processQueryForEachChannel := func(key interface{}, value interface{}) bool {
1097 groupAddr := key.(string)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301098 igd.ProcessQuery(cntx, net.ParseIP(groupAddr), version)
Naveen Sampath04696f72022-06-13 15:19:14 +05301099 return true
1100 }
1101 igd.GroupChannels.Range(processQueryForEachChannel)
1102 ig.IgmpGroupLock.Unlock()
1103 return true
1104 }
1105 va.IgmpGroups.Range(processquery)
1106}
1107
1108// isIgmpJoin to check if it is igmp join
1109func isIgmpJoin(recordType layers.IGMPv3GroupRecordType, sourceAddr []net.IP) bool {
1110 var join = false
1111
1112 if (layers.IGMPToEx == recordType) || (layers.IGMPIsEx == recordType) {
1113 join = true
1114 } else if layers.IGMPBlock == recordType {
1115 if len(sourceAddr) == 0 {
1116 join = true
1117 }
1118 } else if (layers.IGMPToIn == recordType) || (layers.IGMPIsIn == recordType) || (layers.IGMPAllow == recordType) {
1119 if len(sourceAddr) != 0 {
1120 join = true
1121 }
1122 }
1123 return join
1124}
1125
1126func isIncl(recordType layers.IGMPv3GroupRecordType) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +05301127 if (layers.IGMPToIn == recordType) || (layers.IGMPIsIn == recordType) || (layers.IGMPAllow == recordType) {
1128 return true
1129 }
1130 return false
1131}
1132
1133// IgmpProcessPkt to process the IGMP packet received. The packet received brings along with it
1134// the port on which the packet is received and the device the port is in.
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301135func (va *VoltApplication) IgmpProcessPkt(cntx context.Context, device string, port string, pkt gopacket.Packet) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301136 igmpl := pkt.Layer(layers.LayerTypeIGMP)
1137 if igmpl == nil {
1138 logger.Error(ctx, "Invalid IGMP packet arrived as IGMP packet")
1139 return
1140 }
1141 if igmp, ok := igmpl.(*layers.IGMPv1or2); ok {
1142 // This is an IGMPv2 packet.
1143 logger.Debugw(ctx, "IGMPv2 Packet Received", log.Fields{"IPAddr": igmp.GroupAddress})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301144 va.ProcessIgmpv2Pkt(cntx, device, port, pkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301145 return
1146 }
1147 if igmpv3, ok := igmpl.(*layers.IGMP); ok {
1148 logger.Debugw(ctx, "IGMPv3 Packet Received", log.Fields{"NumOfGroups": igmpv3.NumberOfGroupRecords})
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301149 va.ProcessIgmpv3Pkt(cntx, device, port, pkt)
Naveen Sampath04696f72022-06-13 15:19:14 +05301150 }
1151}
1152
1153// IgmpPacketInd for igmp packet indication
1154func (va *VoltApplication) IgmpPacketInd(device string, port string, pkt gopacket.Packet) {
1155 pt := NewIgmpPacketTask(device, port, pkt)
1156 va.IgmpTasks.AddTask(pt)
1157}
1158
Naveen Sampath04696f72022-06-13 15:19:14 +05301159// storeMvlansMap to store mvlan map
1160func (va *VoltApplication) storeMvlansMap(mvlan of.VlanType, name string, mvp *MvlanProfile) {
1161 va.MvlanProfilesByTag.Store(mvlan, mvp)
1162 va.MvlanProfilesByName.Store(name, mvp)
1163}
1164
1165// deleteMvlansMap to delete mvlan map
1166func (va *VoltApplication) deleteMvlansMap(mvlan of.VlanType, name string) {
1167 va.MvlanProfilesByTag.Delete(mvlan)
1168 va.MvlanProfilesByName.Delete(name)
1169}
1170
1171// RestoreMvlansFromDb to read from the DB and restore all the MVLANs
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301172func (va *VoltApplication) RestoreMvlansFromDb(cntx context.Context) {
1173 mvlans, _ := db.GetMvlans(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301174 for _, mvlan := range mvlans {
1175 b, ok := mvlan.Value.([]byte)
1176 if !ok {
1177 logger.Warn(ctx, "The value type is not []byte")
1178 continue
1179 }
1180 var mvp MvlanProfile
1181 err := json.Unmarshal(b, &mvp)
1182 if err != nil {
1183 logger.Warn(ctx, "Unmarshal of MVLAN failed")
1184 continue
1185 }
1186 va.storeMvlansMap(mvp.Mvlan, mvp.Name, &mvp)
1187
1188 for srNo := range mvp.DevicesList {
1189 if mvp.IgmpServVersion[srNo] == nil {
1190 servVersion := IgmpVersion0
1191 mvp.IgmpServVersion[srNo] = &servVersion
1192 }
1193 }
1194 logger.Infow(ctx, "Restored Mvlan Profile", log.Fields{"MVPName": mvp.Name})
1195 }
1196}
1197
1198// GetMvlanProfileByTag fetches MVLAN profile based on the MC VLAN
1199func (va *VoltApplication) GetMvlanProfileByTag(vlan of.VlanType) *MvlanProfile {
1200 if mvp, ok := va.MvlanProfilesByTag.Load(vlan); ok {
1201 return mvp.(*MvlanProfile)
1202 }
1203 return nil
1204}
1205
1206// GetMvlanProfileByName fetches MVLAN profile based on the profile name.
1207func (va *VoltApplication) GetMvlanProfileByName(name string) *MvlanProfile {
1208 if mvp, ok := va.MvlanProfilesByName.Load(name); ok {
1209 return mvp.(*MvlanProfile)
1210 }
1211 return nil
1212}
1213
vinokuma926cb3e2023-03-29 11:41:06 +05301214// UpdateMvlanProfile - only channel groups be updated
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301215func (va *VoltApplication) UpdateMvlanProfile(cntx context.Context, name string, vlan of.VlanType, groups map[string][]string, activeChannelCount uint32, proxy map[string]common.MulticastGroupProxy) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301216 mvpIntf, ok := va.MvlanProfilesByName.Load(name)
1217 if !ok {
1218 logger.Error(ctx, "Update Mvlan Failed: Profile does not exist")
1219 return errors.New("MVLAN profile not found")
1220 }
1221 mvp := mvpIntf.(*MvlanProfile)
1222 // check if groups are same then just update the OLTSerial numbers, push the config on new serial numbers
1223
1224 existingGroup := mvp.Groups
1225 existingProxy := mvp.Proxy
1226 mvp.Groups = make(map[string]*MvlanGroup)
1227 mvp.Proxy = make(map[string]*MCGroupProxy)
1228
1229 /* Need to protect groups and proxy write lock */
1230 mvp.mvpLock.Lock()
1231 for grpName, grpIPList := range groups {
1232 mvp.AddMvlanGroup(grpName, grpIPList)
1233 }
1234 for grpName, proxyInfo := range proxy {
1235 mvp.AddMvlanProxy(grpName, proxyInfo)
1236 }
1237 if _, ok := mvp.Groups[common.StaticGroup]; ok {
1238 if _, yes := mvp.Proxy[common.StaticGroup]; !yes {
1239 mvp.Groups[common.StaticGroup].IsStatic = true
1240 }
1241 }
1242 prevMaxActiveChannels := mvp.MaxActiveChannels
1243 if reflect.DeepEqual(mvp.Groups, existingGroup) && reflect.DeepEqual(mvp.Proxy, existingProxy) {
1244 logger.Info(ctx, "No change in groups config")
1245 if uint32(activeChannelCount) != mvp.MaxActiveChannels {
1246 mvp.MaxActiveChannels = uint32(activeChannelCount)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301247 if err := mvp.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301248 logger.Errorw(ctx, "Mvlan profile Write to DB failed", log.Fields{"ProfileName": mvp.Name})
1249 }
1250 if prevMaxActiveChannels != mvp.MaxActiveChannels {
1251 mvp.UpdateActiveChannelSubscriberAlarm()
1252 }
1253 }
1254 mvp.mvpLock.Unlock()
1255 return nil
1256 }
1257 mvp.mvpLock.Unlock()
1258 mvp.MaxActiveChannels = uint32(activeChannelCount)
1259
1260 // Status is maintained so that in the event of any crash or reboot during update,
1261 // the recovery is possible once the pod is UP again
1262 mvp.SetUpdateStatus("", UpdateInProgress)
1263 mvp.oldGroups = existingGroup
1264 mvp.oldProxy = existingProxy
1265 va.storeMvlansMap(vlan, name, mvp)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301266 if err := mvp.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301267 logger.Errorw(ctx, "Mvlan profile Write to DB failed", log.Fields{"ProfileName": mvp.Name})
1268 }
1269 if prevMaxActiveChannels != mvp.MaxActiveChannels {
1270 mvp.UpdateActiveChannelSubscriberAlarm()
1271 }
1272
1273 // The update task is added as part of Igm p task list, so that any parallel igmp pkt processing is avoided
1274 // Until, the update operation is completed, the igmp pkt processing will be enqueued
1275 updateTask := NewUpdateMvlanTask(mvp, "")
1276 va.IgmpTasks.AddTask(updateTask)
1277 return nil
1278}
1279
1280// isDeviceInList to check if device is the list
1281func isDeviceInList(serialNum string, OLTSerialNums []string) bool {
1282 for _, oltSerialNum := range OLTSerialNums {
1283 if serialNum == oltSerialNum {
1284 return true
1285 }
1286 }
1287 return false
1288}
1289
1290// McastConfigKey creates the key using the olt serial number and mvlan profile id
1291func McastConfigKey(oltSerialNum string, mvlanProfID string) string {
1292 return oltSerialNum + "_" + mvlanProfID
1293}
1294
1295// GetMcastConfig to get McastConfig Information by OLT and Mvlan Profile ID
1296func (va *VoltApplication) GetMcastConfig(oltSerialNum string, mvlanProfID string) *McastConfig {
1297 if mc, ok := va.McastConfigMap.Load(McastConfigKey(oltSerialNum, mvlanProfID)); ok {
1298 return mc.(*McastConfig)
1299 }
1300 return nil
1301}
1302
1303func (va *VoltApplication) storeMcastConfig(oltSerialNum string, mvlanProfID string, mcastConfig *McastConfig) {
1304 va.McastConfigMap.Store(McastConfigKey(oltSerialNum, mvlanProfID), mcastConfig)
1305}
1306
1307func (va *VoltApplication) deleteMcastConfig(oltSerialNum string, mvlanProfID string) {
1308 va.McastConfigMap.Delete(McastConfigKey(oltSerialNum, mvlanProfID))
1309}
1310
1311// AddMcastConfig for addition of a MVLAN profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301312func (va *VoltApplication) AddMcastConfig(cntx context.Context, MvlanProfileID string, IgmpProfileID string, IgmpProxyIP string, OltSerialNum string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301313 var mcastCfg *McastConfig
1314
1315 mcastCfg = va.GetMcastConfig(OltSerialNum, MvlanProfileID)
1316 if mcastCfg == nil {
1317 mcastCfg = &McastConfig{}
1318 } else {
1319 logger.Debugw(ctx, "Mcast Config already exists", log.Fields{"OltSerialNum": mcastCfg.OltSerialNum,
1320 "MVLAN Profile ID": mcastCfg.MvlanProfileID})
1321 }
1322
1323 // Update all igds available
1324 mvpIntf, ok := va.MvlanProfilesByName.Load(MvlanProfileID)
1325 if !ok {
1326 return errors.New("MVLAN profile not found during add mcast config")
1327 }
1328 mvlan := mvpIntf.(*MvlanProfile).Mvlan
1329
1330 mcastCfg.OltSerialNum = OltSerialNum
1331 mcastCfg.MvlanProfileID = MvlanProfileID
1332 mcastCfg.IgmpProfileID = IgmpProfileID
1333 mcastCfg.IgmpProxyIP = net.ParseIP(IgmpProxyIP)
1334
1335 proxyCfg := va.getIgmpProfileMap(IgmpProfileID)
1336
1337 iterIgmpGroups := func(key interface{}, value interface{}) bool {
1338 ig := value.(*IgmpGroup)
1339 if ig.Mvlan != mvlan {
1340 return true
1341 }
1342
1343 for _, igd := range ig.Devices {
1344 if igd.SerialNo != OltSerialNum {
1345 continue
1346 }
1347 igd.proxyCfg = proxyCfg
1348 if IgmpProfileID == "" {
1349 igd.IgmpProxyIP = &igd.proxyCfg.IgmpSourceIP
1350 } else {
1351 igd.IgmpProxyIP = &mcastCfg.IgmpProxyIP
1352 }
1353 mcastCfg.IgmpGroupDevices.Store(igd.GroupID, igd)
1354 logger.Debugw(ctx, "Igd updated with proxyCfg and proxyIP", log.Fields{"name": igd.GroupName,
1355 "IgmpProfileID": IgmpProfileID, "ProxyIP": mcastCfg.IgmpProxyIP})
1356 }
1357 return true
1358 }
1359 va.IgmpGroups.Range(iterIgmpGroups)
1360
1361 va.storeMcastConfig(OltSerialNum, MvlanProfileID, mcastCfg)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301362 if err := mcastCfg.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301363 logger.Errorw(ctx, "McastConfig Write to DB failed", log.Fields{"OltSerialNum": mcastCfg.OltSerialNum, "MvlanProfileID": mcastCfg.MvlanProfileID})
1364 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301365 va.addOltToMvlan(cntx, MvlanProfileID, OltSerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301366
1367 return nil
1368}
1369
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301370func (va *VoltApplication) addOltToMvlan(cntx context.Context, MvlanProfileID string, OltSerialNum string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301371 var mvp *MvlanProfile
1372 if mvpIntf, ok := va.MvlanProfilesByName.Load(MvlanProfileID); ok {
1373 servVersion := IgmpVersion0
1374 mvp = mvpIntf.(*MvlanProfile)
1375 mvp.DevicesList[OltSerialNum] = NoOp
1376 mvp.IgmpServVersion[OltSerialNum] = &servVersion
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301377 if err := mvp.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301378 logger.Errorw(ctx, "Mvlan profile Write to DB failed", log.Fields{"ProfileName": mvp.Name})
1379 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301380 mvp.pushIgmpMcastFlows(cntx, OltSerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301381 }
1382}
1383
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301384func (va *VoltApplication) delOltFromMvlan(cntx context.Context, MvlanProfileID string, OltSerialNum string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301385 var mvp *MvlanProfile
1386 if mvpIntf, ok := va.MvlanProfilesByName.Load(MvlanProfileID); ok {
1387 mvp = mvpIntf.(*MvlanProfile)
vinokuma926cb3e2023-03-29 11:41:06 +05301388 // Delete from mvp list
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301389 mvp.removeIgmpMcastFlows(cntx, OltSerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301390 delete(mvp.DevicesList, OltSerialNum)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301391 if err := mvp.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301392 logger.Errorw(ctx, "Mvlan profile Write to DB failed", log.Fields{"ProfileName": mvp.Name})
1393 }
1394 }
1395}
1396
1397// DelMcastConfig for addition of a MVLAN profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301398func (va *VoltApplication) DelMcastConfig(cntx context.Context, MvlanProfileID string, IgmpProfileID string, IgmpProxyIP string, OltSerialNum string) {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301399 va.delOltFromMvlan(cntx, MvlanProfileID, OltSerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301400 va.deleteMcastConfig(OltSerialNum, MvlanProfileID)
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301401 _ = db.DelMcastConfig(cntx, McastConfigKey(OltSerialNum, MvlanProfileID))
Tinoj Joseph50d722c2022-12-06 22:53:22 +05301402 if d, _ := va.GetDeviceBySerialNo(OltSerialNum); d != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301403 if mvp := va.GetMvlanProfileByName(MvlanProfileID); mvp != nil {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301404 va.RemoveGroupsFromPendingPool(cntx, d.Name, mvp.Mvlan)
Naveen Sampath04696f72022-06-13 15:19:14 +05301405 }
1406 }
1407}
1408
1409// DelAllMcastConfig for deletion of all mcast config
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301410func (va *VoltApplication) DelAllMcastConfig(cntx context.Context, OltSerialNum string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301411 deleteIndividualMcastConfig := func(key interface{}, value interface{}) bool {
1412 mcastCfg := value.(*McastConfig)
1413 if mcastCfg.OltSerialNum == OltSerialNum {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301414 va.DelMcastConfig(cntx, mcastCfg.MvlanProfileID, mcastCfg.IgmpProfileID, mcastCfg.IgmpProxyIP.String(), mcastCfg.OltSerialNum)
Naveen Sampath04696f72022-06-13 15:19:14 +05301415 }
1416 return true
1417 }
1418 va.McastConfigMap.Range(deleteIndividualMcastConfig)
1419 return nil
1420}
1421
1422// UpdateMcastConfig for addition of a MVLAN profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301423func (va *VoltApplication) UpdateMcastConfig(cntx context.Context, MvlanProfileID string, IgmpProfileID string, IgmpProxyIP string, OltSerialNum string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301424 mcastCfg := va.GetMcastConfig(OltSerialNum, MvlanProfileID)
1425 if mcastCfg == nil {
1426 logger.Warnw(ctx, "Mcast Config not found. Unable to update", log.Fields{"Mvlan Profile ID": MvlanProfileID, "OltSerialNum": OltSerialNum})
1427 return nil
1428 }
1429
1430 oldProfID := mcastCfg.IgmpProfileID
1431 mcastCfg.IgmpProfileID = IgmpProfileID
1432 mcastCfg.IgmpProxyIP = net.ParseIP(IgmpProxyIP)
1433
1434 va.storeMcastConfig(OltSerialNum, MvlanProfileID, mcastCfg)
1435
1436 // Update all igds
1437 if oldProfID != mcastCfg.IgmpProfileID {
1438 updateIgdProxyCfg := func(key interface{}, value interface{}) bool {
1439 igd := value.(*IgmpGroupDevice)
1440 igd.proxyCfg = va.getIgmpProfileMap(mcastCfg.IgmpProfileID)
1441 if IgmpProfileID == "" {
1442 igd.IgmpProxyIP = &igd.proxyCfg.IgmpSourceIP
1443 } else {
1444 igd.IgmpProxyIP = &mcastCfg.IgmpProxyIP
1445 }
1446 return true
1447 }
1448 mcastCfg.IgmpGroupDevices.Range(updateIgdProxyCfg)
1449 }
1450
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301451 if err := mcastCfg.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301452 logger.Errorw(ctx, "McastConfig Write to DB failed", log.Fields{"OltSerialNum": mcastCfg.OltSerialNum, "MvlanProfileID": mcastCfg.MvlanProfileID})
1453 }
1454
1455 return nil
1456}
1457
1458// WriteToDb is utility to write Mcast config Info to database
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301459func (mc *McastConfig) WriteToDb(cntx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301460 mc.Version = database.PresentVersionMap[database.McastConfigPath]
1461 b, err := json.Marshal(mc)
1462 if err != nil {
1463 return err
1464 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301465 if err1 := db.PutMcastConfig(cntx, McastConfigKey(mc.OltSerialNum, mc.MvlanProfileID), string(b)); err1 != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301466 return err1
1467 }
1468 return nil
1469}
1470
1471// RestoreMcastConfigsFromDb to read from the DB and restore Mcast configs
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301472func (va *VoltApplication) RestoreMcastConfigsFromDb(cntx context.Context) {
1473 mcastConfigs, _ := db.GetMcastConfigs(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301474 for hash, mcastConfig := range mcastConfigs {
1475 b, ok := mcastConfig.Value.([]byte)
1476 if !ok {
1477 logger.Warn(ctx, "The value type is not []byte")
1478 continue
1479 }
1480 var mc McastConfig
1481 err := json.Unmarshal(b, &mc)
1482 if err != nil {
1483 logger.Warn(ctx, "Unmarshal of Mcast config failed")
1484 continue
1485 }
1486 va.storeMcastConfig(mc.OltSerialNum, mc.MvlanProfileID, &mc)
1487 logger.Infow(ctx, "Restored Mcast config", log.Fields{"OltSerialNum": mc.OltSerialNum, "MvlanProfileID": mc.MvlanProfileID, "hash": hash})
1488 }
1489}
1490
1491// AddMvlanProfile for addition of a MVLAN profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301492func (va *VoltApplication) AddMvlanProfile(cntx context.Context, name string, mvlan of.VlanType, ponVlan of.VlanType,
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301493 groups map[string][]string, isChannelBasedGroup bool, OLTSerialNum []string, activeChannelsPerPon uint32, proxy map[string]common.MulticastGroupProxy) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301494 var mvp *MvlanProfile
1495
1496 if mvp = va.GetMvlanProfileByTag(mvlan); mvp != nil {
1497 logger.Errorw(ctx, "Duplicate MVLAN ID configured", log.Fields{"mvlan": mvlan})
1498 return errors.New("MVLAN profile with same VLANID exists")
1499 }
1500 if mvpIntf, ok := va.MvlanProfilesByName.Load(name); ok {
1501 mvp = mvpIntf.(*MvlanProfile)
1502 for _, serialNum := range OLTSerialNum {
1503 if mvp.DevicesList[serialNum] != Nil {
1504 //This is backup restore scenario, just update the profile
1505 logger.Info(ctx, "Add Mvlan : Profile Name already exists, update-the-profile")
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301506 return va.UpdateMvlanProfile(cntx, name, mvlan, groups, activeChannelsPerPon, proxy)
Naveen Sampath04696f72022-06-13 15:19:14 +05301507 }
1508 }
1509 }
1510
1511 if mvp == nil {
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301512 mvp = NewMvlanProfile(name, mvlan, ponVlan, isChannelBasedGroup, OLTSerialNum, activeChannelsPerPon)
Naveen Sampath04696f72022-06-13 15:19:14 +05301513 }
1514
1515 va.storeMvlansMap(mvlan, name, mvp)
1516
1517 /* Need to protect groups and proxy write lock */
1518 mvp.mvpLock.Lock()
1519 for grpName, grpInfo := range groups {
1520 mvp.AddMvlanGroup(grpName, grpInfo)
1521 }
1522 for grpName, proxyInfo := range proxy {
1523 mvp.AddMvlanProxy(grpName, proxyInfo)
1524 }
1525 if _, ok := mvp.Groups[common.StaticGroup]; ok {
1526 if _, yes := mvp.Proxy[common.StaticGroup]; !yes {
1527 mvp.Groups[common.StaticGroup].IsStatic = true
1528 }
1529 }
1530
1531 logger.Debugw(ctx, "Added MVLAN Profile", log.Fields{"MVLAN": mvp.Mvlan, "PonVlan": mvp.PonVlan, "Name": mvp.Name, "Grp IPs": mvp.Groups, "IsPonVlanPresent": mvp.IsPonVlanPresent})
1532 mvp.mvpLock.Unlock()
1533
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301534 if err := mvp.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301535 logger.Errorw(ctx, "Mvlan profile Write to DB failed", log.Fields{"ProfileName": mvp.Name})
1536 }
1537
1538 return nil
1539}
1540
Naveen Sampath04696f72022-06-13 15:19:14 +05301541// GetMvlanProfileForMcIP - Get an MVLAN profile for a given MC IP. This is used when an
1542// IGMP report is received from the PON port. The MVLAN profile
1543// located is used to idnetify the MC VLAN used in upstream for
1544// join/leave
1545func (va *VoltApplication) GetMvlanProfileForMcIP(profileName string, ip net.IP) (*MvlanProfile, string) {
1546 if mvpIntf, ok := va.MvlanProfilesByName.Load(profileName); ok {
1547 mvp := mvpIntf.(*MvlanProfile)
1548 if grpName := mvp.GetMvlanGroup(ip); grpName != "" {
1549 return mvp, grpName
1550 }
1551 } else {
1552 logger.Warnw(ctx, "Mvlan Profile not found for given profile name", log.Fields{"Profile": profileName})
1553 }
1554 return nil, ""
1555}
1556
Naveen Sampath04696f72022-06-13 15:19:14 +05301557// IgmpTick for igmp tick info
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301558func (va *VoltApplication) IgmpTick(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301559 tickCount++
1560 if (tickCount % 1000) == 0 {
1561 logger.Debugw(ctx, "Time @ Tick", log.Fields{"Tick": tickCount, "Time": time.Now()})
1562 }
1563 igmptick := func(key interface{}, value interface{}) bool {
1564 ig := value.(*IgmpGroup)
1565 if ig.NumDevicesActive() != 0 {
1566 if tickCount%10 == ig.Hash()%10 {
1567 ig.IgmpGroupLock.Lock()
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301568 ig.Tick(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301569 ig.IgmpGroupLock.Unlock()
1570 if ig.NumDevicesActive() == 0 {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301571 va.DelIgmpGroup(cntx, ig)
Naveen Sampath04696f72022-06-13 15:19:14 +05301572 }
1573 }
1574 }
1575 return true
1576 }
1577 va.IgmpGroups.Range(igmptick)
1578}
1579
1580// Tick to add Tick Task
1581func (va *VoltApplication) Tick() {
1582 tt := NewTickTask()
1583 va.IgmpTasks.AddTask(tt)
1584 // va.IgmpTick()
1585}
1586
vinokuma926cb3e2023-03-29 11:41:06 +05301587// AddIgmpProfile for addition of IGMP Profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301588func (va *VoltApplication) AddIgmpProfile(cntx context.Context, igmpProfileConfig *common.IGMPConfig) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301589 var igmpProfile *IgmpProfile
1590
1591 if igmpProfileConfig.ProfileID == DefaultIgmpProfID {
1592 logger.Info(ctx, "Updating default IGMP profile")
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301593 return va.UpdateIgmpProfile(cntx, igmpProfileConfig)
Naveen Sampath04696f72022-06-13 15:19:14 +05301594 }
1595
1596 igmpProfile = va.checkIgmpProfileMap(igmpProfileConfig.ProfileID)
1597 if igmpProfile == nil {
1598 igmpProfile = newIgmpProfile(igmpProfileConfig)
1599 } else {
1600 logger.Errorw(ctx, "IGMP profile already exists", log.Fields{"IgmpProfile": igmpProfileConfig.ProfileID})
1601 return errors.New("IGMP Profile already exists")
1602 }
1603
1604 va.storeIgmpProfileMap(igmpProfileConfig.ProfileID, igmpProfile)
1605
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301606 if err := igmpProfile.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301607 logger.Errorw(ctx, "Igmp profile Write to DB failed", log.Fields{"profileID": igmpProfile.ProfileID})
1608 }
1609
1610 return nil
1611}
1612
Naveen Sampath04696f72022-06-13 15:19:14 +05301613// checkIgmpProfileMap to get Igmp Profile. If not found return nil
1614func (va *VoltApplication) checkIgmpProfileMap(name string) *IgmpProfile {
1615 if igmpProfileIntf, ok := va.IgmpProfilesByName.Load(name); ok {
1616 return igmpProfileIntf.(*IgmpProfile)
1617 }
1618 return nil
1619}
1620
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301621func (va *VoltApplication) resetIgmpProfileToDefault(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301622 igmpProf := va.getIgmpProfileMap(DefaultIgmpProfID)
1623 defIgmpProf := newDefaultIgmpProfile()
1624
1625 igmpProf.UnsolicitedTimeOut = defIgmpProf.UnsolicitedTimeOut
1626 igmpProf.MaxResp = defIgmpProf.MaxResp
1627 igmpProf.KeepAliveInterval = defIgmpProf.KeepAliveInterval
1628 igmpProf.KeepAliveCount = defIgmpProf.KeepAliveCount
1629 igmpProf.LastQueryInterval = defIgmpProf.LastQueryInterval
1630 igmpProf.LastQueryCount = defIgmpProf.LastQueryCount
1631 igmpProf.FastLeave = defIgmpProf.FastLeave
1632 igmpProf.PeriodicQuery = defIgmpProf.PeriodicQuery
1633 igmpProf.IgmpCos = defIgmpProf.IgmpCos
1634 igmpProf.WithRAUpLink = defIgmpProf.WithRAUpLink
1635 igmpProf.WithRADownLink = defIgmpProf.WithRADownLink
1636 igmpProf.IgmpVerToServer = defIgmpProf.IgmpVerToServer
1637 igmpProf.IgmpSourceIP = defIgmpProf.IgmpSourceIP
1638
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301639 if err := igmpProf.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301640 logger.Errorw(ctx, "Igmp profile Write to DB failed", log.Fields{"profileID": igmpProf.ProfileID})
1641 }
1642}
1643
1644// getIgmpProfileMap to get Igmp Profile. If not found return default IGMP config
1645func (va *VoltApplication) getIgmpProfileMap(name string) *IgmpProfile {
1646 if igmpProfileIntf, ok := va.IgmpProfilesByName.Load(name); ok {
1647 return igmpProfileIntf.(*IgmpProfile)
1648 }
1649
1650 // There will be always a default igmp profile.
1651 defaultIgmpProfileIntf, _ := va.IgmpProfilesByName.Load(DefaultIgmpProfID)
1652 return defaultIgmpProfileIntf.(*IgmpProfile)
1653}
1654
1655// storeIgmpProfileMap to store Igmp Profile
1656func (va *VoltApplication) storeIgmpProfileMap(name string, igmpProfile *IgmpProfile) {
1657 va.IgmpProfilesByName.Store(name, igmpProfile)
1658}
1659
1660// deleteIgmpProfileMap to delete Igmp Profile
1661func (va *VoltApplication) deleteIgmpProfileMap(name string) {
1662 va.IgmpProfilesByName.Delete(name)
1663}
1664
Akash Sonia8246972023-01-03 10:37:08 +05301665// TODO - DelIgmpProfile for deleting IGMP Profile based on profile Id
1666// func (va *VoltApplication) DelIgmpProfile(cntx context.Context, igmpProfileConfig *common.IGMPConfig) error {
1667// // Deletion of default igmp profile is blocked from submgr. Keeping additional check for safety.
1668// if igmpProfileConfig.ProfileID == DefaultIgmpProfID {
1669// logger.Info(ctx, "Resetting default IGMP profile")
1670// va.resetIgmpProfileToDefault(cntx)
1671// return nil
1672// }
1673// igmpProfile := va.checkIgmpProfileMap(igmpProfileConfig.ProfileID)
1674// if igmpProfile == nil {
1675// logger.Warnw(ctx, "Igmp Profile not found. Unable to delete", log.Fields{"Profile ID": igmpProfileConfig.ProfileID})
1676// return nil
1677// }
1678
1679// va.deleteIgmpProfileMap(igmpProfileConfig.ProfileID)
1680
1681// _ = db.DelIgmpProfile(cntx, igmpProfileConfig.ProfileID)
1682
1683// return nil
1684// }
1685
1686// DelIgmpProfile for deleting IGMP Profile based on profile Id
1687func (va *VoltApplication) DelIgmpProfile(cntx context.Context, profileID string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301688 // Deletion of default igmp profile is blocked from submgr. Keeping additional check for safety.
Akash Sonia8246972023-01-03 10:37:08 +05301689 if profileID == DefaultIgmpProfID {
Naveen Sampath04696f72022-06-13 15:19:14 +05301690 logger.Info(ctx, "Resetting default IGMP profile")
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301691 va.resetIgmpProfileToDefault(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301692 return nil
1693 }
Akash Sonia8246972023-01-03 10:37:08 +05301694 igmpProfile := va.checkIgmpProfileMap(profileID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301695 if igmpProfile == nil {
Akash Sonia8246972023-01-03 10:37:08 +05301696 logger.Warnw(ctx, "Igmp Profile not found. Unable to delete", log.Fields{"Profile ID": profileID})
Naveen Sampath04696f72022-06-13 15:19:14 +05301697 return nil
1698 }
1699
Akash Sonia8246972023-01-03 10:37:08 +05301700 va.deleteIgmpProfileMap(profileID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301701
Akash Sonia8246972023-01-03 10:37:08 +05301702 err := db.DelIgmpProfile(cntx, profileID)
1703 if err != nil {
1704 logger.Errorw(ctx, "Failed to delete Igmp profile from DB", log.Fields{"Error": err})
1705 return err
1706 }
Naveen Sampath04696f72022-06-13 15:19:14 +05301707
1708 return nil
1709}
1710
vinokuma926cb3e2023-03-29 11:41:06 +05301711// UpdateIgmpProfile for addition of IGMP Profile
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301712func (va *VoltApplication) UpdateIgmpProfile(cntx context.Context, igmpProfileConfig *common.IGMPConfig) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301713 igmpProfile := va.checkIgmpProfileMap(igmpProfileConfig.ProfileID)
1714 if igmpProfile == nil {
1715 logger.Errorw(ctx, "Igmp Profile not found. Unable to update", log.Fields{"Profile ID": igmpProfileConfig.ProfileID})
1716 return errors.New("IGMP Profile not found")
1717 }
1718
1719 igmpProfile.ProfileID = igmpProfileConfig.ProfileID
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301720 igmpProfile.UnsolicitedTimeOut = igmpProfileConfig.UnsolicitedTimeOut
1721 igmpProfile.MaxResp = igmpProfileConfig.MaxResp
Naveen Sampath04696f72022-06-13 15:19:14 +05301722
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301723 keepAliveInterval := igmpProfileConfig.KeepAliveInterval
Naveen Sampath04696f72022-06-13 15:19:14 +05301724
vinokuma926cb3e2023-03-29 11:41:06 +05301725 // KeepAliveInterval should have a min of 10 seconds
Naveen Sampath04696f72022-06-13 15:19:14 +05301726 if keepAliveInterval < MinKeepAliveInterval {
1727 keepAliveInterval = MinKeepAliveInterval
1728 logger.Infow(ctx, "Auto adjust keepAliveInterval - Value < 10", log.Fields{"Received": igmpProfileConfig.KeepAliveInterval, "Configured": keepAliveInterval})
1729 }
1730 igmpProfile.KeepAliveInterval = keepAliveInterval
1731
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301732 igmpProfile.KeepAliveCount = igmpProfileConfig.KeepAliveCount
1733 igmpProfile.LastQueryInterval = igmpProfileConfig.LastQueryInterval
1734 igmpProfile.LastQueryCount = igmpProfileConfig.LastQueryCount
Naveen Sampath04696f72022-06-13 15:19:14 +05301735 igmpProfile.FastLeave = *igmpProfileConfig.FastLeave
1736 igmpProfile.PeriodicQuery = *igmpProfileConfig.PeriodicQuery
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +05301737 igmpProfile.IgmpCos = igmpProfileConfig.IgmpCos
Naveen Sampath04696f72022-06-13 15:19:14 +05301738 igmpProfile.WithRAUpLink = *igmpProfileConfig.WithRAUpLink
1739 igmpProfile.WithRADownLink = *igmpProfileConfig.WithRADownLink
1740
1741 if igmpProfileConfig.IgmpVerToServer == "2" || igmpProfileConfig.IgmpVerToServer == "v2" {
1742 igmpProfile.IgmpVerToServer = "2"
1743 } else {
1744 igmpProfile.IgmpVerToServer = "3"
1745 }
1746
1747 if igmpProfileConfig.IgmpSourceIP != "" {
1748 igmpProfile.IgmpSourceIP = net.ParseIP(igmpProfileConfig.IgmpSourceIP)
1749 }
1750
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301751 if err := igmpProfile.WriteToDb(cntx); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301752 logger.Errorw(ctx, "Igmp profile Write to DB failed", log.Fields{"profileID": igmpProfile.ProfileID})
1753 }
1754
1755 return nil
1756}
1757
1758// RestoreIGMPProfilesFromDb to read from the DB and restore IGMP Profiles
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301759func (va *VoltApplication) RestoreIGMPProfilesFromDb(cntx context.Context) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301760 // Loading IGMP profiles
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301761 igmpProfiles, _ := db.GetIgmpProfiles(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301762 for _, igmpProfile := range igmpProfiles {
1763 b, ok := igmpProfile.Value.([]byte)
1764 if !ok {
1765 logger.Warn(ctx, "The value type is not []byte")
1766 continue
1767 }
1768 var igmpProf IgmpProfile
1769 err := json.Unmarshal(b, &igmpProf)
1770 if err != nil {
1771 logger.Warn(ctx, "Unmarshal of IGMP Profile failed")
1772 continue
1773 }
1774 va.storeIgmpProfileMap(igmpProf.ProfileID, &igmpProf)
1775 logger.Infow(ctx, "Restored Igmp Profile", log.Fields{"Conf": igmpProf})
1776 }
1777}
1778
1779// InitIgmpSrcMac for initialization of igmp source mac
1780func (va *VoltApplication) InitIgmpSrcMac() {
1781 srcMac, err := getPodMacAddr()
1782 if err != nil {
1783 igmpSrcMac = "00:11:11:11:11:11"
1784 return
1785 }
1786 igmpSrcMac = srcMac
1787}
1788
Naveen Sampath04696f72022-06-13 15:19:14 +05301789// DelMvlanProfile for deletion of a MVLAN group
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301790func (va *VoltApplication) DelMvlanProfile(cntx context.Context, name string) error {
Naveen Sampath04696f72022-06-13 15:19:14 +05301791 if mvpIntf, ok := va.MvlanProfilesByName.Load(name); ok {
1792 mvp := mvpIntf.(*MvlanProfile)
1793
1794 if len(mvp.DevicesList) == 0 {
1795 mvp.DeleteInProgress = true
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301796 mvp.DelFromDb(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +05301797 va.deleteMvlansMap(mvp.Mvlan, name)
1798 logger.Debugw(ctx, "Deleted MVLAN Profile", log.Fields{"Name": mvp.Name})
1799 } else {
1800 logger.Errorw(ctx, "Unable to delete Mvlan Profile as there is still an OLT attached to it", log.Fields{"Name": mvp.Name,
1801 "Device List": mvp.DevicesList})
1802 return errors.New("MVLAN attached to devices")
1803 }
1804
1805 return nil
1806 }
1807 logger.Errorw(ctx, "MVLAN Profile not found", log.Fields{"MvlanProfile Name": name})
1808 return nil
1809}
1810
1811// ReceiverUpInd for receiver up indication
1812func (va *VoltApplication) ReceiverUpInd(device string, port string, mvpName string, vlan of.VlanType, pbits []of.PbitType) {
1813 logger.Infow(ctx, "Receiver Indication: UP", log.Fields{"device": device, "port": port, "MVP": mvpName, "vlan": vlan, "pbits": pbits})
1814 if mvpIntf, ok := va.MvlanProfilesByName.Load(mvpName); ok {
1815 mvp := mvpIntf.(*MvlanProfile)
1816 if devIntf, ok := va.DevicesDisc.Load(device); ok {
1817 dev := devIntf.(*VoltDevice)
1818 proxyCfg, proxyIP, _ := getIgmpProxyCfgAndIP(mvp.Mvlan, dev.SerialNum)
1819 for _, pbit := range pbits {
1820 sendGeneralQuery(device, port, vlan, uint8(pbit), proxyCfg, proxyIP)
1821 }
1822 } else {
1823 logger.Warnw(ctx, "Device not found for given port", log.Fields{"device": device, "port": port})
1824 }
1825 } else {
1826 logger.Warnw(ctx, "Mvlan Profile not found for given profileName", log.Fields{"MVP": mvpName, "vlan": vlan})
1827 }
1828}
1829
1830// sendGeneralQuery to send general query
1831func sendGeneralQuery(device string, port string, cVlan of.VlanType, pbit uint8, proxyCfg *IgmpProfile, proxyIP *net.IP) {
Tinoj Josephcf161be2022-07-07 19:47:47 +05301832 if queryPkt, err := Igmpv2QueryPacket(AllSystemsMulticastGroupIP, cVlan, *proxyIP, pbit, proxyCfg.MaxResp); err == nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301833 if err := cntlr.GetController().PacketOutReq(device, port, port, queryPkt, false); err != nil {
1834 logger.Warnw(ctx, "General Igmpv2 Query Failed to send", log.Fields{"Device": device, "Port": port, "Packet": queryPkt, "Pbit": pbit})
1835 } else {
1836 logger.Debugw(ctx, "General Igmpv2 Query Sent", log.Fields{"Device": device, "Port": port, "Packet": queryPkt, "Pbit": pbit})
1837 }
1838 }
1839 if getVersion(proxyCfg.IgmpVerToServer) == IgmpVersion3 {
Tinoj Josephcf161be2022-07-07 19:47:47 +05301840 if queryPkt, err := Igmpv3QueryPacket(AllSystemsMulticastGroupIP, cVlan, *proxyIP, pbit, proxyCfg.MaxResp); err == nil {
Naveen Sampath04696f72022-06-13 15:19:14 +05301841 if err := cntlr.GetController().PacketOutReq(device, port, port, queryPkt, false); err != nil {
1842 logger.Warnw(ctx, "General Igmpv3 Query Failed to send", log.Fields{"Device": device, "Port": port, "Packet": queryPkt, "Pbit": pbit})
1843 } else {
1844 logger.Debugw(ctx, "General Igmpv3 Query Sent", log.Fields{"Device": device, "Port": port, "Packet": queryPkt, "Pbit": pbit})
1845 }
1846 }
1847 }
1848}
1849
1850// ReceiverDownInd to send receiver down indication
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301851func (va *VoltApplication) ReceiverDownInd(cntx context.Context, device string, port string) {
Naveen Sampath04696f72022-06-13 15:19:14 +05301852 logger.Infow(ctx, " Receiver Indication: DOWN", log.Fields{"device": device, "port": port})
1853
1854 ponPortID := va.GetPonPortID(device, port)
1855
1856 del := func(key interface{}, value interface{}) bool {
1857 ig := value.(*IgmpGroup)
1858 ig.IgmpGroupLock.Lock()
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301859 ig.DelReceiveronDownInd(cntx, device, port, ponPortID)
Naveen Sampath04696f72022-06-13 15:19:14 +05301860 ig.IgmpGroupLock.Unlock()
1861 if ig.NumDevicesActive() == 0 {
Tinoj Joseph07cc5372022-07-18 22:53:51 +05301862 va.DelIgmpGroup(cntx, ig)
Naveen Sampath04696f72022-06-13 15:19:14 +05301863 }
1864 return true
1865 }
1866 va.IgmpGroups.Range(del)
1867}