#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
+#include <sys/socket.h>
#include "virnetlink.h"
#include "logging.h"
#include "virmacaddr.h"
#include "virterror_internal.h"
+#ifndef SOL_NETLINK
+# define SOL_NETLINK 270
+#endif
+
#define VIR_FROM_THIS VIR_FROM_NET
#define NETLINK_ACK_TIMEOUT_S 2
records in this multiple */
# define NETLINK_EVENT_ALLOC_EXTENT 10
-static virNetlinkEventSrvPrivatePtr server = NULL;
+/* Linux kernel supports up to MAX_LINKS (32 at the time) individual
+ * netlink protocols. */
+static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL};
static virNetlinkHandle *placeholder_nlhandle = NULL;
/* Function definitions */
* @respbuf: pointer to pointer where response buffer will be allocated
* @respbuflen: pointer to integer holding the size of the response buffer
* on return of the function.
- * @nl_pid: the pid of the process to talk to, i.e., pid = 0 for kernel
+ * @src_pid: the pid of the process to send a message
+ * @dst_pid: the pid of the process to talk to, i.e., pid = 0 for kernel
+ * @protocol: netlink protocol
+ * @groups: the group identifier
*
* Send the given message to the netlink layer and receive response.
* Returns 0 on success, -1 on error. In case of error, no response
*/
int virNetlinkCommand(struct nl_msg *nl_msg,
unsigned char **respbuf, unsigned int *respbuflen,
- uint32_t src_pid, uint32_t dst_pid)
+ uint32_t src_pid, uint32_t dst_pid,
+ unsigned int protocol, unsigned int groups)
{
int rc = 0;
struct sockaddr_nl nladdr = {
int fd;
int n;
struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg);
- virNetlinkHandle *nlhandle = virNetlinkAlloc();
+ virNetlinkHandle *nlhandle = NULL;
+
+ if (protocol >= MAX_LINKS) {
+ virReportSystemError(EINVAL,
+ _("invalid protocol argument: %d"), protocol);
+ return -EINVAL;
+ }
+ nlhandle = virNetlinkAlloc();
if (!nlhandle) {
virReportSystemError(errno,
"%s", _("cannot allocate nlhandle for netlink"));
return -1;
}
- if (nl_connect(nlhandle, NETLINK_ROUTE) < 0) {
+ if (nl_connect(nlhandle, protocol) < 0) {
+ virReportSystemError(errno,
+ _("cannot connect to netlink socket with protocol %d"),
+ protocol);
+ rc = -1;
+ goto error;
+ }
+
+ fd = nl_socket_get_fd(nlhandle);
+ if (fd < 0) {
+ virReportSystemError(errno,
+ "%s", _("cannot get netlink socket fd"));
+ rc = -1;
+ goto error;
+ }
+
+ if (groups && nl_socket_add_membership(nlhandle, groups) < 0) {
virReportSystemError(errno,
- "%s", _("cannot connect to netlink socket"));
+ "%s", _("cannot add netlink membership"));
rc = -1;
goto error;
}
goto error;
}
- fd = nl_socket_get_fd(nlhandle);
-
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
* virNetlinkEventRemoveClientPrimitive:
*
* @i: index of the client to remove from the table
+ * @protocol: netlink protocol
*
* This static function does the low level removal of a client from
* the table once its index is known, including calling the remove
*
* assumes success, returns nothing.
*/
-static void
-virNetlinkEventRemoveClientPrimitive(size_t i)
+static int
+virNetlinkEventRemoveClientPrimitive(size_t i, unsigned int protocol)
{
- virNetlinkEventRemoveCallback removeCB = server->handles[i].removeCB;
+ if (protocol >= MAX_LINKS)
+ return -EINVAL;
+
+ virNetlinkEventRemoveCallback removeCB = server[protocol]->handles[i].removeCB;
if (removeCB) {
- (removeCB)(server->handles[i].watch,
- &server->handles[i].macaddr,
- server->handles[i].opaque);
+ (removeCB)(server[protocol]->handles[i].watch,
+ &server[protocol]->handles[i].macaddr,
+ server[protocol]->handles[i].opaque);
}
- server->handles[i].deleted = VIR_NETLINK_HANDLE_DELETED;
+ server[protocol]->handles[i].deleted = VIR_NETLINK_HANDLE_DELETED;
+ return 0;
}
static void
* stop the monitor to receive netlink messages for libvirtd.
* This removes the netlink socket fd from the event handler.
*
+ * @protocol: netlink protocol
+ *
* Returns -1 if the monitor cannot be unregistered, 0 upon success
*/
int
-virNetlinkEventServiceStop(void)
+virNetlinkEventServiceStop(unsigned int protocol)
{
- virNetlinkEventSrvPrivatePtr srv = server;
+ if (protocol >= MAX_LINKS)
+ return -EINVAL;
+
+ virNetlinkEventSrvPrivatePtr srv = server[protocol];
int i;
VIR_INFO("stopping netlink event service");
- if (!server)
+ if (!server[protocol])
return 0;
virNetlinkEventServerLock(srv);
/* free any remaining clients on the list */
for (i = 0; i < srv->handlesCount; i++) {
if (srv->handles[i].deleted == VIR_NETLINK_HANDLE_VALID)
- virNetlinkEventRemoveClientPrimitive(i);
+ virNetlinkEventRemoveClientPrimitive(i, protocol);
}
- server = 0;
+ server[protocol] = NULL;
virNetlinkEventServerUnlock(srv);
virMutexDestroy(&srv->lock);
*
* Returns if the netlink event service is running.
*
+ * @protocol: netlink protocol
+ *
* Returns 'true' if the service is running, 'false' if stopped.
*/
bool
-virNetlinkEventServiceIsRunning(void)
+virNetlinkEventServiceIsRunning(unsigned int protocol)
{
- return server != NULL;
+ if (protocol >= MAX_LINKS) {
+ virReportSystemError(EINVAL,
+ _("invalid protocol argument: %d"), protocol);
+ return false;
+ }
+
+ return server[protocol] != NULL;
}
/**
* virNetlinkEventServiceLocalPid:
*
+ * @protocol: netlink protocol
+ *
* Returns the nl_pid value that was used to bind() the netlink socket
* used by the netlink event service, or -1 on error (netlink
* guarantees that this value will always be > 0).
*/
-int virNetlinkEventServiceLocalPid(void)
+int virNetlinkEventServiceLocalPid(unsigned int protocol)
{
- if (!(server && server->netlinknh)) {
+ if (protocol >= MAX_LINKS)
+ return -EINVAL;
+
+ if (!(server[protocol] && server[protocol]->netlinknh)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("netlink event service not running"));
return -1;
}
- return (int)nl_socket_get_local_port(server->netlinknh);
+ return (int)nl_socket_get_local_port(server[protocol]->netlinknh);
}
* start a monitor to receive netlink messages for libvirtd.
* This registers a netlink socket with the event interface.
*
+ * @protocol: netlink protocol
+ * @groups: broadcast groups to join in
* Returns -1 if the monitor cannot be registered, 0 upon success
*/
int
-virNetlinkEventServiceStart(void)
+virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
{
virNetlinkEventSrvPrivatePtr srv;
int fd;
int ret = -1;
- if (server)
+ if (protocol >= MAX_LINKS) {
+ virReportSystemError(EINVAL,
+ _("invalid protocol argument: %d"), protocol);
+ return -EINVAL;
+ }
+
+ if (server[protocol])
return 0;
- VIR_INFO("starting netlink event service");
+ VIR_INFO("starting netlink event service with protocol %d", protocol);
if (VIR_ALLOC(srv) < 0) {
virReportOOMError();
goto error_locked;
}
- if (nl_connect(srv->netlinknh, NETLINK_ROUTE) < 0) {
+ if (nl_connect(srv->netlinknh, protocol) < 0) {
virReportSystemError(errno,
- "%s", _("cannot connect to netlink socket"));
+ _("cannot connect to netlink socket with protocol %d"), protocol);
goto error_server;
}
fd = nl_socket_get_fd(srv->netlinknh);
-
if (fd < 0) {
virReportSystemError(errno,
"%s", _("cannot get netlink socket fd"));
goto error_server;
}
+ if (groups && nl_socket_add_membership(srv->netlinknh, groups) < 0) {
+ virReportSystemError(errno,
+ "%s", _("cannot add netlink membership"));
+ goto error_server;
+ }
+
if (nl_socket_set_nonblocking(srv->netlinknh)) {
virReportSystemError(errno, "%s",
_("cannot set netlink socket nonblocking"));
VIR_DEBUG("netlink event listener on fd: %i running", fd);
ret = 0;
- server = srv;
+ server[protocol] = srv;
error_server:
if (ret < 0) {
* @opaque: user data to pass to callback
* @macaddr: macaddr to store with the data. Used to identify callers.
* May be null.
+ * @protocol: netlink protocol
*
* register a callback for handling of netlink messages. The
* registered function receives the entire netlink message and
int
virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
virNetlinkEventRemoveCallback removeCB,
- void *opaque, const virMacAddrPtr macaddr)
+ void *opaque, const virMacAddrPtr macaddr,
+ unsigned int protocol)
{
int i, r, ret = -1;
- virNetlinkEventSrvPrivatePtr srv = server;
+ virNetlinkEventSrvPrivatePtr srv = NULL;
+
+ if (protocol >= MAX_LINKS)
+ return -EINVAL;
+
+ srv = server[protocol];
if (handleCB == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
*
* @watch: watch whose handle to remove
* @macaddr: macaddr whose handle to remove
+ * @protocol: netlink protocol
*
* Unregister a callback from a netlink monitor.
* The handler function referenced will no longer receive netlink messages.
* Returns -1 if the file handle was not registered, 0 upon success
*/
int
-virNetlinkEventRemoveClient(int watch, const virMacAddrPtr macaddr)
+virNetlinkEventRemoveClient(int watch, const virMacAddrPtr macaddr,
+ unsigned int protocol)
{
int i;
int ret = -1;
- virNetlinkEventSrvPrivatePtr srv = server;
+ virNetlinkEventSrvPrivatePtr srv = NULL;
+
+ if (protocol >= MAX_LINKS)
+ return -EINVAL;
+
+ srv = server[protocol];
VIR_DEBUG("removing client watch=%d, mac=%p.", watch, macaddr);
VIR_DEBUG("removed client: %d by %s.",
srv->handles[i].watch, watch ? "index" : "mac");
- virNetlinkEventRemoveClientPrimitive(i);
+ virNetlinkEventRemoveClientPrimitive(i, protocol);
ret = 0;
goto cleanup;
}
unsigned char **respbuf ATTRIBUTE_UNUSED,
unsigned int *respbuflen ATTRIBUTE_UNUSED,
uint32_t src_pid ATTRIBUTE_UNUSED,
- uint32_t dst_pid ATTRIBUTE_UNUSED)
+ uint32_t dst_pid ATTRIBUTE_UNUSED,
+ unsigned int protocol ATTRIBUTE_UNUSED,
+ unsigned int groups ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
* stopNetlinkEventServer: stop the monitor to receive netlink
* messages for libvirtd
*/
-int virNetlinkEventServiceStop(void)
+int virNetlinkEventServiceStop(unsigned int protocol ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
return 0;
* startNetlinkEventServer: start a monitor to receive netlink
* messages for libvirtd
*/
-int virNetlinkEventServiceStart(void)
+int virNetlinkEventServiceStart(unsigned int protocol ATTRIBUTE_UNUSED,
+ unsigned int groups ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
return 0;
* virNetlinkEventServiceIsRunning: returns if the netlink event
* service is running.
*/
-bool virNetlinkEventServiceIsRunning(void)
+bool virNetlinkEventServiceIsRunning(unsigned int protocol ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return 0;
}
-int virNetlinkEventServiceLocalPid(void)
+int virNetlinkEventServiceLocalPid(unsigned int protocol ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
virNetlinkEventRemoveCallback removeCB ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED,
const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
+ unsigned int protocol ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
*/
int virNetlinkEventRemoveClient(int watch ATTRIBUTE_UNUSED,
const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
+ unsigned int protocol ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;