Skip to content
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

adding ipv6 support and del/modify to p4runtime_lib #326

Closed
wants to merge 1 commit into from
Closed

adding ipv6 support and del/modify to p4runtime_lib #326

wants to merge 1 commit into from

Conversation

mc36
Copy link

@mc36 mc36 commented Jan 17, 2020

No description provided.

@@ -35,15 +36,25 @@ 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})$')
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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fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::))))$')
Copy link
Member

Choose a reason for hiding this comment

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

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:

@jafingerhut
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants