1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 #include <arpa/inet.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
33 #include "openflow-netlink.h"
37 #include "socket-util.h"
42 #define THIS_MODULE VLM_VCONN_NETLINK
50 static struct netlink_vconn *
51 netlink_vconn_cast(struct vconn *vconn)
53 assert(vconn->class == &netlink_vconn_class);
54 return CONTAINER_OF(vconn, struct netlink_vconn, vconn);
58 netlink_open(const char *name, char *suffix, struct vconn **vconnp)
60 struct netlink_vconn *netlink;
64 if (sscanf(suffix, "%d", &dp_idx) != 1) {
65 fatal(0, "%s: bad peer name format", name);
68 netlink = xmalloc(sizeof *netlink);
69 netlink->vconn.class = &netlink_vconn_class;
70 retval = dpif_open(dp_idx, true, &netlink->dp);
76 *vconnp = &netlink->vconn;
81 netlink_close(struct vconn *vconn)
83 struct netlink_vconn *netlink = netlink_vconn_cast(vconn);
84 dpif_close(&netlink->dp);
89 netlink_prepoll(struct vconn *vconn, int want, struct pollfd *pfd)
91 struct netlink_vconn *netlink = netlink_vconn_cast(vconn);
92 pfd->fd = nl_sock_fd(netlink->dp.sock);
93 if (want & WANT_RECV) {
94 pfd->events |= POLLIN;
96 if (want & WANT_SEND) {
97 pfd->events |= POLLOUT;
102 netlink_recv(struct vconn *vconn, struct buffer **bufferp)
104 struct netlink_vconn *netlink = netlink_vconn_cast(vconn);
105 return dpif_recv_openflow(&netlink->dp, bufferp, false);
109 netlink_send(struct vconn *vconn, struct buffer *buffer)
111 struct netlink_vconn *netlink = netlink_vconn_cast(vconn);
112 int retval = dpif_send_openflow(&netlink->dp, buffer, false);
114 buffer_delete(buffer);
119 struct vconn_class netlink_vconn_class = {
121 .open = netlink_open,
122 .close = netlink_close,
123 .prepoll = netlink_prepoll,
124 .recv = netlink_recv,
125 .send = netlink_send,