|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "k8s.io/klog/v2" |
| 10 | + |
| 11 | + "github.com/kubeovn/kube-ovn/pkg/ovs" |
| 12 | + "github.com/kubeovn/kube-ovn/pkg/util" |
| 13 | +) |
| 14 | + |
| 15 | +const flowSyncPeriod = 15 * time.Second |
| 16 | + |
| 17 | +var managedFlowCookieSet = map[uint64]struct{}{ |
| 18 | + util.UnderlaySvcLocalOpenFlowCookieV4: {}, |
| 19 | + util.UnderlaySvcLocalOpenFlowCookieV6: {}, |
| 20 | +} |
| 21 | + |
| 22 | +func (c *Controller) requestFlowSync() { |
| 23 | + if c.flowChan == nil { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + select { |
| 28 | + case c.flowChan <- struct{}{}: |
| 29 | + klog.V(5).Info("OpenFlow sync requested") |
| 30 | + default: |
| 31 | + klog.V(5).Info("OpenFlow sync already requested") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (c *Controller) syncFlows() { |
| 36 | + if c.ovsClient == nil { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + flowsByBridge := c.storeFlowCache() |
| 41 | + |
| 42 | + bridges, err := ovs.Bridges() |
| 43 | + if err != nil { |
| 44 | + klog.Errorf("failed to list bridges: %v", err) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + for _, bridgeName := range bridges { |
| 49 | + existing, err := ovs.DumpFlows(c.ovsClient, bridgeName) |
| 50 | + if err != nil { |
| 51 | + klog.Errorf("failed to dump flows for bridge %s: %v", bridgeName, err) |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + preserved := filterUnmanagedFlows(existing) |
| 56 | + managed := flowsByBridge[bridgeName] |
| 57 | + merged := append(preserved, managed...) |
| 58 | + |
| 59 | + if err := ovs.ReplaceFlows(bridgeName, merged); err != nil { |
| 60 | + klog.Errorf("failed to replace flows for bridge %s: %v", bridgeName, err) |
| 61 | + continue |
| 62 | + } |
| 63 | + if len(managed) == 0 { |
| 64 | + klog.V(5).Infof("no managed flows for bridge %s", bridgeName) |
| 65 | + continue |
| 66 | + } |
| 67 | + klog.V(3).Infof("synced %d managed flows on bridge %s", len(managed), bridgeName) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (c *Controller) storeFlowCache() map[string][]string { |
| 72 | + snapshot := make(map[string][]string) |
| 73 | + |
| 74 | + c.flowCacheMutex.RLock() |
| 75 | + defer c.flowCacheMutex.RUnlock() |
| 76 | + |
| 77 | + appendFlowCache(snapshot, c.flowCache) |
| 78 | + |
| 79 | + return snapshot |
| 80 | +} |
| 81 | + |
| 82 | +func appendFlowCache(dst map[string][]string, src map[string]map[string][]string) { |
| 83 | + for bridgeName, entries := range src { |
| 84 | + if len(entries) == 0 { |
| 85 | + if _, ok := dst[bridgeName]; !ok { |
| 86 | + dst[bridgeName] = nil |
| 87 | + } |
| 88 | + continue |
| 89 | + } |
| 90 | + for _, flows := range entries { |
| 91 | + if len(flows) == 0 { |
| 92 | + if _, ok := dst[bridgeName]; !ok { |
| 93 | + dst[bridgeName] = nil |
| 94 | + } |
| 95 | + continue |
| 96 | + } |
| 97 | + dst[bridgeName] = append(dst[bridgeName], flows...) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func filterUnmanagedFlows(flows []string) []string { |
| 103 | + filtered := make([]string, 0, len(flows)) |
| 104 | + for _, flow := range flows { |
| 105 | + if isManagedFlow(flow) { |
| 106 | + continue |
| 107 | + } |
| 108 | + filtered = append(filtered, flow) |
| 109 | + } |
| 110 | + return filtered |
| 111 | +} |
| 112 | + |
| 113 | +func isManagedFlow(flow string) bool { |
| 114 | + cookie, ok := extractFlowCookie(flow) |
| 115 | + if !ok { |
| 116 | + return false |
| 117 | + } |
| 118 | + _, exists := managedFlowCookieSet[cookie] |
| 119 | + return exists |
| 120 | +} |
| 121 | + |
| 122 | +func extractFlowCookie(flow string) (uint64, bool) { |
| 123 | + idx := strings.Index(flow, "cookie=") |
| 124 | + if idx == -1 { |
| 125 | + return 0, false |
| 126 | + } |
| 127 | + cookieField := flow[idx+len("cookie="):] |
| 128 | + if comma := strings.Index(cookieField, ","); comma != -1 { |
| 129 | + cookieField = cookieField[:comma] |
| 130 | + } |
| 131 | + if slash := strings.Index(cookieField, "/"); slash != -1 { |
| 132 | + cookieField = cookieField[:slash] |
| 133 | + } |
| 134 | + cookieField = strings.TrimSpace(cookieField) |
| 135 | + if cookieField == "" { |
| 136 | + return 0, false |
| 137 | + } |
| 138 | + |
| 139 | + cookie, err := parseHexUint64(cookieField) |
| 140 | + if err != nil { |
| 141 | + return 0, false |
| 142 | + } |
| 143 | + return cookie, true |
| 144 | +} |
| 145 | + |
| 146 | +func parseHexUint64(value string) (uint64, error) { |
| 147 | + if !strings.HasPrefix(value, "0x") { |
| 148 | + value = "0x" + value |
| 149 | + } |
| 150 | + return strconv.ParseUint(value, 0, 64) |
| 151 | +} |
| 152 | + |
| 153 | +func (c *Controller) runFlowSync(stopCh <-chan struct{}) { |
| 154 | + klog.Info("Starting OpenFlow sync loop") |
| 155 | + |
| 156 | + ticker := time.NewTicker(flowSyncPeriod) |
| 157 | + defer ticker.Stop() |
| 158 | + for { |
| 159 | + select { |
| 160 | + case <-ticker.C: |
| 161 | + c.syncFlows() |
| 162 | + case <-c.flowChan: |
| 163 | + klog.V(5).Info("Immediate OpenFlow sync triggered") |
| 164 | + c.syncFlows() |
| 165 | + ticker.Reset(flowSyncPeriod) |
| 166 | + case <-stopCh: |
| 167 | + klog.Info("Stopping OpenFlow sync loop") |
| 168 | + return |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func (c *Controller) getPortID(bridgeName, portName string) (int, error) { |
| 174 | + if c.ovsClient == nil { |
| 175 | + return 0, fmt.Errorf("ovs client not initialized") |
| 176 | + } |
| 177 | + |
| 178 | + portInfo, err := c.ovsClient.OpenFlow.DumpPort(bridgeName, portName) |
| 179 | + if err != nil { |
| 180 | + return 0, fmt.Errorf("failed to dump port %s on bridge %s: %w", portName, bridgeName, err) |
| 181 | + } |
| 182 | + return portInfo.PortID, nil |
| 183 | +} |
| 184 | + |
| 185 | +func (c *Controller) setFlowCache(cache map[string]map[string][]string, bridgeName, key string, flows []string) { |
| 186 | + c.flowCacheMutex.Lock() |
| 187 | + defer c.flowCacheMutex.Unlock() |
| 188 | + |
| 189 | + if cache[bridgeName] == nil { |
| 190 | + cache[bridgeName] = make(map[string][]string) |
| 191 | + } |
| 192 | + cache[bridgeName][key] = flows |
| 193 | +} |
| 194 | + |
| 195 | +func (c *Controller) deleteFlowCache(cache map[string]map[string][]string, bridgeName, key string) { |
| 196 | + c.flowCacheMutex.Lock() |
| 197 | + defer c.flowCacheMutex.Unlock() |
| 198 | + |
| 199 | + entries := cache[bridgeName] |
| 200 | + if entries == nil { |
| 201 | + return |
| 202 | + } |
| 203 | + delete(entries, key) |
| 204 | +} |
0 commit comments