Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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. |
| 14 | */ |
| 15 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | |
| 22 | "github.com/google/gopacket" |
| 23 | ) |
| 24 | |
| 25 | // ------------------------------------------------------------------ |
| 26 | // ********** Tasks |
| 27 | // |
| 28 | // IGMP related tasks which essentially process packets and the ticks |
| 29 | // This is to serailize access to data structures and this also limits |
| 30 | // the amount of CPU consumed. We can bring more capacity by running |
| 31 | // more groups in parallel as we need to add parallelism |
| 32 | |
| 33 | // ----------------------------------------------------------------- |
| 34 | // ** Timer Task ** |
| 35 | // |
| 36 | // Timer processing - Tick is a poke that the IGMP processing receives |
| 37 | // from the timer thread. The entire IGMP processing receives a single |
| 38 | // tick. |
| 39 | |
| 40 | // TickTask structure |
| 41 | type TickTask struct { |
| 42 | ctx context.Context |
| 43 | taskID uint8 |
| 44 | ts string |
| 45 | } |
| 46 | |
| 47 | // NewTickTask is constructor for TickTask |
| 48 | func NewTickTask() *TickTask { |
| 49 | return &TickTask{} |
| 50 | } |
| 51 | |
| 52 | // Name to return the name of the task |
| 53 | func (tt *TickTask) Name() string { |
| 54 | return "Process Tick" |
| 55 | } |
| 56 | |
| 57 | // TaskID to return the task id |
| 58 | func (tt *TickTask) TaskID() uint8 { |
| 59 | return tt.taskID |
| 60 | } |
| 61 | |
| 62 | // Timestamp to return the timestamp of task |
| 63 | func (tt *TickTask) Timestamp() string { |
| 64 | return tt.ts |
| 65 | } |
| 66 | |
| 67 | // Stop to stop the task |
| 68 | func (tt *TickTask) Stop() { |
| 69 | } |
| 70 | |
| 71 | // Start to start the task |
| 72 | func (tt *TickTask) Start(ctx context.Context, taskID uint8) error { |
| 73 | tt.taskID = taskID |
| 74 | tt.ctx = ctx |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 75 | GetApplication().IgmpTick(ctx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 76 | return nil |
| 77 | } |
| 78 | |
| 79 | // --------------------------------------------------------------- |
| 80 | // ** Packet processing Task ** |
| 81 | // |
| 82 | // |
| 83 | |
| 84 | // IgmpPacketTask structure |
| 85 | type IgmpPacketTask struct { |
| 86 | ctx context.Context |
| 87 | taskID uint8 |
| 88 | Device string |
| 89 | Port string |
| 90 | Pkt gopacket.Packet |
| 91 | ts string |
| 92 | } |
| 93 | |
| 94 | // NewIgmpPacketTask is the constructor for IgmpPacketTask |
| 95 | func NewIgmpPacketTask(device string, port string, pkt gopacket.Packet) *IgmpPacketTask { |
| 96 | var pt IgmpPacketTask |
| 97 | pt.Device = device |
| 98 | pt.Port = port |
| 99 | pt.Pkt = pkt |
| 100 | pt.ts = (time.Now()).Format(time.RFC3339Nano) |
| 101 | return &pt |
| 102 | } |
| 103 | |
| 104 | // Name to return name of the task |
| 105 | func (pt *IgmpPacketTask) Name() string { |
| 106 | return "Igmp Packet Task" |
| 107 | } |
| 108 | |
| 109 | // TaskID to return the task id |
| 110 | func (pt *IgmpPacketTask) TaskID() uint8 { |
| 111 | return pt.taskID |
| 112 | } |
| 113 | |
| 114 | // Timestamp to return the timestamp for the task |
| 115 | func (pt *IgmpPacketTask) Timestamp() string { |
| 116 | return pt.ts |
| 117 | } |
| 118 | |
| 119 | // Stop to stop the task |
| 120 | func (pt *IgmpPacketTask) Stop() { |
| 121 | } |
| 122 | |
| 123 | // Start to start the task |
| 124 | func (pt *IgmpPacketTask) Start(ctx context.Context, taskID uint8) error { |
| 125 | pt.taskID = taskID |
| 126 | pt.ctx = ctx |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 127 | GetApplication().IgmpProcessPkt(ctx, pt.Device, pt.Port, pt.Pkt) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 128 | return nil |
| 129 | } |
| 130 | |
| 131 | // UpdateMvlanTask structure |
| 132 | type UpdateMvlanTask struct { |
| 133 | ctx context.Context |
| 134 | taskID uint8 |
| 135 | DeviceID string |
| 136 | mvp *MvlanProfile |
| 137 | ts string |
| 138 | } |
| 139 | |
| 140 | // NewUpdateMvlanTask is the constructor for UpdateMvlanTask |
| 141 | func NewUpdateMvlanTask(mvp *MvlanProfile, deviceID string) *UpdateMvlanTask { |
| 142 | var mt UpdateMvlanTask |
| 143 | mt.mvp = mvp |
| 144 | mt.DeviceID = deviceID |
| 145 | mt.ts = (time.Now()).Format(time.RFC3339Nano) |
| 146 | return &mt |
| 147 | } |
| 148 | |
| 149 | // Name to retun the name of the task |
| 150 | func (mt *UpdateMvlanTask) Name() string { |
| 151 | return "Update Mvlan Task" |
| 152 | } |
| 153 | |
| 154 | // TaskID to return the task id of the task |
| 155 | func (mt *UpdateMvlanTask) TaskID() uint8 { |
| 156 | return mt.taskID |
| 157 | } |
| 158 | |
| 159 | // Timestamp to return the timestamp of the task |
| 160 | func (mt *UpdateMvlanTask) Timestamp() string { |
| 161 | return mt.ts |
| 162 | } |
| 163 | |
| 164 | // Stop to stop the task |
| 165 | func (mt *UpdateMvlanTask) Stop() { |
| 166 | } |
| 167 | |
| 168 | // Start to start the task |
| 169 | func (mt *UpdateMvlanTask) Start(ctx context.Context, taskID uint8) error { |
| 170 | mt.taskID = taskID |
| 171 | mt.ctx = ctx |
| 172 | mvp := mt.mvp |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 173 | mvp.UpdateProfile(ctx, mt.DeviceID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 174 | return nil |
| 175 | } |