json: Move from lib to include/openvswitch.
[cascardo/ovs.git] / lib / netlink-socket.c
index 5cf1027..7502764 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <sys/uio.h>
 #include <unistd.h>
 #include "coverage.h"
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
 #include "hash.h"
-#include "hmap.h"
+#include "openvswitch/hmap.h"
 #include "netlink.h"
 #include "netlink-protocol.h"
 #include "odp-netlink.h"
-#include "ofpbuf.h"
+#include "openvswitch/ofpbuf.h"
 #include "ovs-thread.h"
 #include "poll-loop.h"
 #include "seq.h"
@@ -49,20 +49,6 @@ COVERAGE_DEFINE(netlink_sent);
 #define SOL_NETLINK 270
 #endif
 
-#ifdef _WIN32
-static struct ovs_mutex portid_mutex = OVS_MUTEX_INITIALIZER;
-static uint32_t g_last_portid = 0;
-
-/* Port IDs must be unique! */
-static uint32_t
-portid_next(void)
-    OVS_GUARDED_BY(portid_mutex)
-{
-    g_last_portid++;
-    return g_last_portid;
-}
-#endif /* _WIN32 */
-
 /* A single (bad) Netlink message can in theory dump out many, many log
  * messages, so the burst size is set quite high here to avoid missing useful
  * information.  Also, at high logging levels we log *all* Netlink messages. */
@@ -73,6 +59,7 @@ static void log_nlmsg(const char *function, int error,
                       const void *message, size_t size, int protocol);
 #ifdef _WIN32
 static int get_sock_pid_from_kernel(struct nl_sock *sock);
+static int set_sock_property(struct nl_sock *sock);
 #endif
 \f
 /* Netlink sockets. */
@@ -141,6 +128,7 @@ nl_sock_create(int protocol, struct nl_sock **sockp)
     sock = xmalloc(sizeof *sock);
 
 #ifdef _WIN32
+    sock->overlapped.hEvent = NULL;
     sock->handle = CreateFile(OVS_DEVICE_NAME_USER,
                               GENERIC_READ | GENERIC_WRITE,
                               FILE_SHARE_READ | FILE_SHARE_WRITE,
@@ -178,6 +166,10 @@ nl_sock_create(int protocol, struct nl_sock **sockp)
     if (retval != 0) {
         goto error;
     }
+    retval = set_sock_property(sock);
+    if (retval != 0) {
+        goto error;
+    }
 #else
     if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUFFORCE,
                    &rcvbuf, sizeof rcvbuf)) {
@@ -195,6 +187,7 @@ nl_sock_create(int protocol, struct nl_sock **sockp)
         goto error;
     }
     sock->rcvbuf = retval;
+    retval = 0;
 
     /* Connect to kernel (pid 0) as remote address. */
     memset(&remote, 0, sizeof remote);
@@ -296,6 +289,76 @@ get_sock_pid_from_kernel(struct nl_sock *sock)
 
     return retval;
 }
+
+/* Used for setting and managing socket properties in userspace and kernel.
+ * Currently two attributes are tracked - pid and protocol
+ * protocol - supplied by userspace based on the netlink family. Windows uses
+ *            this property to set the value in kernel datapath.
+ *            eg: (NETLINK_GENERIC/ NETLINK_NETFILTER)
+ * pid -      generated by windows kernel and set in userspace. The property
+ *            is not modified.
+ * Also verify if Protocol and PID in Kernel reflects the values in userspace
+ * */
+static int
+set_sock_property(struct nl_sock *sock)
+{
+    static const struct nl_policy ovs_socket_policy[] = {
+        [OVS_NL_ATTR_SOCK_PROTO] = { .type = NL_A_BE32, .optional = true },
+        [OVS_NL_ATTR_SOCK_PID] = { .type = NL_A_BE32, .optional = true }
+    };
+
+    struct ofpbuf request, *reply;
+    struct ovs_header *ovs_header;
+    struct nlattr *attrs[ARRAY_SIZE(ovs_socket_policy)];
+    int retval = 0;
+    int error;
+
+    ofpbuf_init(&request, 0);
+    nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0,
+                          OVS_CTRL_CMD_SOCK_PROP, OVS_WIN_CONTROL_VERSION);
+    ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header);
+    ovs_header->dp_ifindex = 0;
+
+    nl_msg_put_be32(&request, OVS_NL_ATTR_SOCK_PROTO, sock->protocol);
+    /* pid is already set as part of get_sock_pid_from_kernel()
+     * This is added to maintain consistency
+     */
+    nl_msg_put_be32(&request, OVS_NL_ATTR_SOCK_PID, sock->pid);
+
+    error = nl_sock_transact(sock, &request, &reply);
+    ofpbuf_uninit(&request);
+    if (error) {
+        retval = EINVAL;
+    }
+
+    if (!nl_policy_parse(reply,
+                         NLMSG_HDRLEN + GENL_HDRLEN + sizeof *ovs_header,
+                         ovs_socket_policy, attrs,
+                         ARRAY_SIZE(ovs_socket_policy))) {
+        ofpbuf_delete(reply);
+        retval = EINVAL;
+    }
+    /* Verify if the properties are setup properly */
+    if (attrs[OVS_NL_ATTR_SOCK_PROTO]) {
+        int protocol = nl_attr_get_be32(attrs[OVS_NL_ATTR_SOCK_PROTO]);
+        if (protocol != sock->protocol) {
+            VLOG_ERR("Invalid protocol returned:%d expected:%d",
+                     protocol, sock->protocol);
+            retval = EINVAL;
+        }
+    }
+
+    if (attrs[OVS_NL_ATTR_SOCK_PID]) {
+        int pid = nl_attr_get_be32(attrs[OVS_NL_ATTR_SOCK_PID]);
+        if (pid != sock->pid) {
+            VLOG_ERR("Invalid pid returned:%d expected:%d",
+                     pid, sock->pid);
+            retval = EINVAL;
+        }
+    }
+
+    return retval;
+}
 #endif  /* _WIN32 */
 
 #ifdef _WIN32
@@ -372,8 +435,8 @@ nl_sock_subscribe_packets(struct nl_sock *sock)
 
     error = nl_sock_subscribe_packet__(sock, true);
     if (error) {
-        VLOG_WARN("could not unsubscribe packets (%s)",
-                  ovs_strerror(errno));
+        VLOG_WARN("could not subscribe packets (%s)",
+                  ovs_strerror(error));
         return error;
     }
     sock->read_ioctl = OVS_IOCTL_READ_PACKET;
@@ -388,8 +451,8 @@ nl_sock_unsubscribe_packets(struct nl_sock *sock)
 
     int error = nl_sock_subscribe_packet__(sock, false);
     if (error) {
-        VLOG_WARN("could not subscribe to packets (%s)",
-                  ovs_strerror(errno));
+        VLOG_WARN("could not unsubscribe to packets (%s)",
+                  ovs_strerror(error));
         return error;
     }
 
@@ -567,7 +630,7 @@ nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait)
         if (!DeviceIoControl(sock->handle, sock->read_ioctl,
                              NULL, 0, tail, sizeof tail, &bytes, NULL)) {
             VLOG_DBG_RL(&rl, "fatal driver failure in transact: %s",
-                ovs_lasterror_to_string());
+                        ovs_lasterror_to_string());
             retval = -1;
             /* XXX: Map to a more appropriate error. */
             errno = EINVAL;
@@ -1204,6 +1267,7 @@ pend_io_request(struct nl_sock *sock)
 
     ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header);
     ovs_header->dp_ifindex = 0;
+    nlmsg->nlmsg_len = request.size;
 
     if (!DeviceIoControl(sock->handle, OVS_IOCTL_WRITE,
                          request.data, request.size,
@@ -1800,15 +1864,12 @@ static void
 log_nlmsg(const char *function, int error,
           const void *message, size_t size, int protocol)
 {
-    struct ofpbuf buffer;
-    char *nlmsg;
-
     if (!VLOG_IS_DBG_ENABLED()) {
         return;
     }
 
-    ofpbuf_use_const(&buffer, message, size);
-    nlmsg = nlmsg_to_string(&buffer, protocol);
+    struct ofpbuf buffer = ofpbuf_const_initializer(message, size);
+    char *nlmsg = nlmsg_to_string(&buffer, protocol);
     VLOG_DBG_RL(&rl, "%s (%s): %s", function, ovs_strerror(error), nlmsg);
     free(nlmsg);
 }