-
Notifications
You must be signed in to change notification settings - Fork 390
container: T7186: Add macvlan network type for containers #4686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import hashlib | ||
from socket import AF_INET | ||
from socket import AF_INET6 | ||
from vyos.utils.process import cmd | ||
|
||
def _are_same_ip(one, two): | ||
from socket import inet_pton | ||
|
@@ -48,6 +50,49 @@ def is_netns_interface(interface, netns): | |
return True | ||
return False | ||
|
||
def get_host_identity() -> str: | ||
""" | ||
Build a stable host identity string for deterministic MAC generation. | ||
|
||
Combines: | ||
• The system's HardwareUUID (from /sys/class/dmi/id/product_uuid) | ||
• The system hostname | ||
|
||
Both are normalized (lowercase, dashes removed in UUID) and joined with a colon. | ||
|
||
Returns: | ||
str: A string "<uuid>:<hostname>", used as part of the host-specific seed when | ||
generating deterministic MAC addresses. | ||
""" | ||
uuid = cmd(f"cat /sys/class/dmi/id/product_uuid").strip().replace("-", "").lower() | ||
host = cmd("hostname").strip().lower() | ||
return f"{uuid}:{host}" | ||
|
||
def gen_mac(name: str, addr: str, ident: str) -> str: | ||
""" | ||
Generate a deterministic locally-administered MAC address. | ||
|
||
The MAC is derived from: | ||
• Host identity (UUID + hostname) | ||
• Container name | ||
• Concatenated address string (IPv4 and/or IPv6 addresses) | ||
|
||
A SHA-256 digest is computed from the combined string. The first 5 bytes | ||
of the digest are used, prefixed with 0x02 to mark the address as | ||
locally-administered and unicast. | ||
|
||
Args: | ||
name (str): Container name to differentiate MACs. | ||
addr (str): Concatenated list of container addresses (IPv4/IPv6). | ||
|
||
Returns: | ||
str: Deterministic MAC address in standard "xx:xx:xx:xx:xx:xx" format. | ||
""" | ||
h = hashlib.sha256(f"{ident}:{name}:{addr}".encode()).hexdigest() | ||
# 0x02 = locally-administered, unicast | ||
b = [0x02] + [int(h[i:i+2], 16) for i in range(0, 10, 2)] # 5 bytes = 40 bits | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The magic number Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 10 isn't a magic number, it's the number of digits I need to grab to equal 40bits. Each hex digit is 4 bits, so 10*4=40. I don't think changing this as Copilot suggests is an improvement. |
||
return ":".join(f"{x:02x}" for x in b) | ||
|
||
def get_netns_all() -> list: | ||
from json import loads | ||
from vyos.utils.process import cmd | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These commands are executed every time
gen_mac()
is called throughget_host_identity()
. Since host identity rarely changes during runtime, consider caching the result to avoid repeated subprocess calls.Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, there's no need to get that data per-container. I'll make that change.