Skip to content

add openflow sync refer to ovn-kubernetes#6115

Closed
changluyi wants to merge 2 commits intorelease-1.14from
add_open_flow_sync
Closed

add openflow sync refer to ovn-kubernetes#6115
changluyi wants to merge 2 commits intorelease-1.14from
add_open_flow_sync

Conversation

@changluyi
Copy link
Copy Markdown
Collaborator

@changluyi changluyi commented Dec 30, 2025

Pull Request

What type of this PR

  • Refactor OpenFlow sync to use atomic ovs-ofctl --bundle replace-flows while preserving non-managed flows, and reorganize flow rule/cache logic
    for clearer layering and naming.

Behavior

  • Sync interval: 15s (ovn-k8s aligned) with immediate trigger on flow updates.
  • Managed flows are replaced atomically; unmanaged flows are retained.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. feature New network feature labels Dec 30, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @changluyi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a robust OpenFlow synchronization mechanism, inspired by ovn-kubernetes, to manage network flows more effectively. It transitions from direct, incremental OpenFlow rule modifications to a cache-based system where desired rules are maintained locally. A dedicated background process now periodically, or on demand, reconciles this cache with the actual OpenFlow state on OVS bridges, employing an atomic replacement strategy to ensure network consistency and resilience.

Highlights

  • OpenFlow Synchronization Loop: A new background goroutine, runFlowSync, has been introduced in the controller's main Run method to manage OpenFlow rules, ensuring continuous reconciliation of network flows.
  • Flow Caching Mechanism: A local flowCache (a map protected by a sync.RWMutex) and a flowChan (for immediate sync triggers) have been added to the controller. This cache stores the desired OpenFlow rules, moving away from direct, immediate OVS modifications.
  • Atomic Flow Replacement: The method of applying OpenFlow rules has been fundamentally changed. Instead of individual add/delete operations, the system now uses ovs-ofctl replace-flows to atomically update all managed flows on a bridge, improving consistency and robustness.
  • Refactored OpenFlow Management: Direct OVS OpenFlow manipulation functions (AddOrUpdateUnderlaySubnetSvcLocalOpenFlow, DeleteUnderlaySubnetSvcLocalOpenFlow) have been removed from pkg/ovs/ovs-ofctl.go and replaced by the new cache-based, synchronized system.
  • Windows Compatibility Stubs: Placeholder functions for syncFlows and runFlowSync have been added for Windows, indicating that this new OpenFlow synchronization feature is currently Linux-specific.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the OpenFlow rule management by introducing a synchronization loop, similar to the approach in ovn-kubernetes. Instead of applying OpenFlow rules directly, changes are now cached and then atomically applied to OVS using ovs-ofctl replace-flows. This improves robustness by avoiding inconsistent states. The changes are well-structured, introducing new files for flow rule management and the synchronization logic. My review focuses on improving the maintainability and robustness of the new implementation.

Comment on lines +122 to +144
func extractFlowCookie(flow string) (uint64, bool) {
idx := strings.Index(flow, "cookie=")
if idx == -1 {
return 0, false
}
cookieField := flow[idx+len("cookie="):]
if comma := strings.Index(cookieField, ","); comma != -1 {
cookieField = cookieField[:comma]
}
if slash := strings.Index(cookieField, "/"); slash != -1 {
cookieField = cookieField[:slash]
}
cookieField = strings.TrimSpace(cookieField)
if cookieField == "" {
return 0, false
}

cookie, err := parseHexUint64(cookieField)
if err != nil {
return 0, false
}
return cookie, true
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of extractFlowCookie relies on strings.Index to find the cookie= field. The order of fields in ovs-ofctl dump-flows output is not guaranteed, so cookie= might not be at the beginning. This could lead to incorrect parsing if another field contains cookie=. A more robust approach is to parse the flow string as a set of key-value pairs by splitting by comma.

func extractFlowCookie(flow string) (uint64, bool) {
	for _, field := range strings.Split(flow, ",") {
		field = strings.TrimSpace(field)
		if !strings.HasPrefix(field, "cookie=") {
			continue
		}

		cookieField := strings.TrimPrefix(field, "cookie=")
		if slash := strings.Index(cookieField, "/"); slash != -1 {
			cookieField = cookieField[:slash]
		}

		cookie, err := parseHexUint64(cookieField)
		if err != nil {
			return 0, false
		}
		return cookie, true
	}
	return 0, false
}

Comment on lines +82 to +100
func appendFlowCache(dst map[string][]string, src map[string]map[string][]string) {
for bridgeName, entries := range src {
if len(entries) == 0 {
if _, ok := dst[bridgeName]; !ok {
dst[bridgeName] = nil
}
continue
}
for _, flows := range entries {
if len(flows) == 0 {
if _, ok := dst[bridgeName]; !ok {
dst[bridgeName] = nil
}
continue
}
dst[bridgeName] = append(dst[bridgeName], flows...)
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic in appendFlowCache can be simplified for better readability and maintainability. The current implementation has some redundant checks. A more concise version can achieve the same result by ensuring the bridge key exists in the destination map and then appending flows.

func appendFlowCache(dst map[string][]string, src map[string]map[string][]string) {
	for bridgeName, entries := range src {
		// Ensure key exists for each bridge in src, initializing to nil if not present.
		if _, ok := dst[bridgeName]; !ok {
			dst[bridgeName] = nil
		}
		for _, flows := range entries {
			dst[bridgeName] = append(dst[bridgeName], flows...)
		}
	}
}

@coveralls
Copy link
Copy Markdown

coveralls commented Dec 30, 2025

Pull Request Test Coverage Report for Build 20589963075

Details

  • 0 of 223 (0.0%) changed or added relevant lines in 5 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.05%) to 21.405%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/daemon/controller.go 0 2 0.0%
pkg/daemon/controller_linux.go 0 7 0.0%
pkg/ovs/ovs-ofctl.go 0 19 0.0%
pkg/daemon/flow_rules_linux.go 0 62 0.0%
pkg/daemon/flow_sync_linux.go 0 133 0.0%
Files with Coverage Reduction New Missed Lines %
pkg/daemon/controller_linux.go 1 0.0%
Totals Coverage Status
Change from base Build 20568902709: -0.05%
Covered Lines: 10637
Relevant Lines: 49693

💛 - Coveralls

Signed-off-by: clyi <clyi@alauda.io>
Signed-off-by: clyi <clyi@alauda.io>
@changluyi changluyi closed this Dec 30, 2025
@oilbeater oilbeater deleted the add_open_flow_sync branch February 24, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New network feature size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants