adding ipv6 support and del/modify to p4runtime_lib#326
Closed
mc36 wants to merge 1 commit intop4lang:masterfrom
mc36:master
Closed
adding ipv6 support and del/modify to p4runtime_lib#326mc36 wants to merge 1 commit intop4lang:masterfrom mc36:master
mc36 wants to merge 1 commit intop4lang:masterfrom
mc36:master
Conversation
rst0git
reviewed
Jan 26, 2020
| return ':'.join(s.encode('hex') for s in encoded_mac_addr) | ||
|
|
||
| ip_pattern = re.compile('^(\d{1,3}\.){3}(\d{1,3})$') | ||
| ipv6_pattern = re.compile('^(?:(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-fA-F]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:(?:[0-9a-fA-F]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,1}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:(?:[0-9a-fA-F]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,2}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:(?:[0-9a-fA-F]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,3}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:[0-9a-fA-F]{1,4})):)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,4}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,5}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::))))$') |
Member
There was a problem hiding this comment.
I'm not sure if using regex pattern to match IPv6 addresses is a good idea because the regex itself is complex and it would be very difficult to debug. An alternative solution could be:
diff --git a/utils/p4runtime_lib/convert.py b/utils/p4runtime_lib/convert.py
index f6580e0..ca452ee 100644
--- a/utils/p4runtime_lib/convert.py
+++ b/utils/p4runtime_lib/convert.py
@@ -35,14 +35,17 @@ def decodeMac(encoded_mac_addr):
return ':'.join(s.encode('hex') for s in encoded_mac_addr)
ip_pattern = re.compile('^(\d{1,3}\.){3}(\d{1,3})$')
-def matchesIPv4(ip_addr_string):
- return ip_pattern.match(ip_addr_string) is not None
-
-def encodeIPv4(ip_addr_string):
- return socket.inet_aton(ip_addr_string)
-
-def decodeIPv4(encoded_ip_addr):
- return socket.inet_ntoa(encoded_ip_addr)
+def encodeIP(ip_addr_string):
+ if ip_pattern.match(ip_addr_string):
+ return socket.inet_pton(socket.AF_INET, ip_addr_string)
+ return socket.inet_pton(socket.AF_INET6, ip_addr_string)
+
+def decodeIP(encoded_ip_addr):
+ if len(encoded_ip_addr) == 4:
+ return socket.inet_ntop(socket.AF_INET, encoded_ip_addr)
+ if len(encoded_ip_addr) == 16:
+ return socket.inet_ntop(socket.AF_INET6, encoded_ip_addr)
+ raise Exception("Encoded IP address has invalid length %d" % len(encoded_ip_addr))
def bitwidthToBytes(bitwidth):
return int(math.ceil(bitwidth / 8.0))
@@ -66,11 +69,12 @@ def encode(x, bitwidth):
if type(x) == str:
if matchesMac(x):
encoded_bytes = encodeMac(x)
- elif matchesIPv4(x):
- encoded_bytes = encodeIPv4(x)
else:
- # Assume that the string is already encoded
- encoded_bytes = x
+ try:
+ encoded_bytes = encodeIP(x)
+ except socket.error:
+ # Assume that the string is already encoded
+ encoded_bytes = x
elif type(x) == int:
encoded_bytes = encodeNum(x, bitwidth)
else:
Collaborator
|
@Abhinavcode13 pointed out that this PR's changes should already be mostly covered by #580, and the ability to delete table entries was added in a separate PR some time probably years ago, and recently created #601 adds an updated method for modifying table entries. Closing this issue in favor of those otehrs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.