Merge changes I82fd7793,Id3cca05d

* changes:
  Make sock_diag_test.py pass on 4.9 devices that don't have SCTP.
  Make BPF tests pass on device.
diff --git a/net/test/bpf.py b/net/test/bpf.py
index 9e8cf1e..a96a95a 100755
--- a/net/test/bpf.py
+++ b/net/test/bpf.py
@@ -15,15 +15,20 @@
 # limitations under the License.
 
 import ctypes
+import os
 
 import csocket
 import cstruct
 import net_test
 import socket
 
-# TODO: figure out how to make this arch-dependent if we run these tests
-# on non-X86
-__NR_bpf = 321
+# __NR_bpf syscall numbers for various architectures.
+# TODO: is there a better way of doing this?
+__NR_bpf = {
+    "aarch64": 280,
+    "armv8l": 386,
+    "x86_64": 321}[os.uname()[4]]
+
 LOG_LEVEL = 1
 LOG_SIZE = 65536
 
@@ -159,7 +164,7 @@
 
 # BPF program syscalls
 def BpfSyscall(op, attr):
-  ret = libc.syscall(__NR_bpf, op, attr.CPointer(), len(attr))
+  ret = libc.syscall(__NR_bpf, op, csocket.VoidPointer(attr), len(attr))
   csocket.MaybeRaiseSocketError(ret)
   return ret
 
@@ -214,9 +219,9 @@
 
 # Attach a socket eBPF filter to a target socket
 def BpfProgAttachSocket(sock_fd, prog_fd):
-  prog_ptr = ctypes.c_uint32(prog_fd)
+  uint_fd = ctypes.c_uint32(prog_fd)
   ret = libc.setsockopt(sock_fd, socket.SOL_SOCKET, SO_ATTACH_BPF,
-                        ctypes.addressof(prog_ptr), ctypes.sizeof(prog_ptr))
+                        ctypes.pointer(uint_fd), ctypes.sizeof(uint_fd))
   csocket.MaybeRaiseSocketError(ret)
 
 # Attach a eBPF filter to a cgroup
diff --git a/net/test/bpf_test.py b/net/test/bpf_test.py
index f0505a7..7014da4 100755
--- a/net/test/bpf_test.py
+++ b/net/test/bpf_test.py
@@ -19,6 +19,8 @@
 import os
 import socket
 import struct
+import subprocess
+import tempfile
 import unittest
 
 from bpf import *  # pylint: disable=wildcard-import
@@ -281,15 +283,22 @@
 
   @classmethod
   def setUpClass(cls):
-    if not os.path.isdir("/tmp"):
-      os.mkdir('/tmp')
-    os.system('mount -t cgroup2 cg_bpf /tmp')
-    cls._cg_fd = os.open('/tmp', os.O_DIRECTORY | os.O_RDONLY)
+    cls._cg_dir = tempfile.mkdtemp(prefix="cg_bpf-")
+    cmd = "mount -t cgroup2 cg_bpf %s" % cls._cg_dir
+    try:
+      subprocess.check_call(cmd.split())
+    except subprocess.CalledProcessError:
+      # If an exception is thrown in setUpClass, the test fails and
+      # tearDownClass is not called.
+      os.rmdir(cls._cg_dir)
+      raise
+    cls._cg_fd = os.open(cls._cg_dir, os.O_DIRECTORY | os.O_RDONLY)
 
   @classmethod
   def tearDownClass(cls):
     os.close(cls._cg_fd)
-    os.system('umount cg_bpf')
+    subprocess.call(('umount %s' % cls._cg_dir).split())
+    os.rmdir(cls._cg_dir)
 
   def setUp(self):
     self.prog_fd = -1
diff --git a/net/test/sock_diag_test.py b/net/test/sock_diag_test.py
index 5a7bf18..9419587 100755
--- a/net/test/sock_diag_test.py
+++ b/net/test/sock_diag_test.py
@@ -36,6 +36,7 @@
 NO_BYTECODE = ""
 HAVE_SO_COOKIE_SUPPORT = net_test.LINUX_VERSION >= (4, 9, 0)
 
+IPPROTO_SCTP = 132
 
 def HaveUdpDiag():
   # There is no way to tell whether a dump succeeded: if the appropriate handler
@@ -50,8 +51,18 @@
   s.close()
   return have_udp_diag
 
+def HaveSctp():
+  if net_test.LINUX_VERSION < (4, 7, 0):
+    return False
+  try:
+    s = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)
+    s.close()
+    return True
+  except IOError:
+    return False
 
 HAVE_UDP_DIAG = HaveUdpDiag()
+HAVE_SCTP = HaveSctp()
 
 
 class SockDiagBaseTest(multinetwork_base.MultiNetworkBaseTest):
@@ -899,8 +910,6 @@
         d545cac net: inet: diag: expose the socket mark to privileged processes.
   """
 
-  IPPROTO_SCTP = 132
-
   def FilterEstablishedSockets(self, mark, mask):
     instructions = [(sock_diag.INET_DIAG_BC_MARK_COND, 1, 2, (mark, mask))]
     bytecode = self.sock_diag.PackBytecode(instructions)
@@ -1023,14 +1032,13 @@
         s.close()
 
       # Basic test for SCTP. sctp_diag was only added in 4.7.
-      if net_test.LINUX_VERSION >= (4, 7, 0):
-        s = socket(family, SOCK_STREAM, self.IPPROTO_SCTP)
+      if HAVE_SCTP:
+        s = socket(family, SOCK_STREAM, IPPROTO_SCTP)
         s.bind((addr, 0))
         s.listen(1)
         mark = self.SetRandomMark(s)
         self.assertSocketMarkIs(s, mark)
-        sockets = self.sock_diag.DumpAllInetSockets(self.IPPROTO_SCTP,
-                                                    NO_BYTECODE)
+        sockets = self.sock_diag.DumpAllInetSockets(IPPROTO_SCTP, NO_BYTECODE)
         self.assertEqual(1, len(sockets))
         self.assertEqual(mark, sockets[0][1].get("INET_DIAG_MARK", None))
         s.close()