1 /*
2  * Copyright (C) 2010-2013, 2015 Red Hat, Inc.
3  * Copyright (C) 2010-2012 IBM Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library.  If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include "internal.h"
23 #include "virmacaddr.h"
24 
25 #if defined(WITH_LIBNL)
26 
27 # include <netlink/msg.h>
28 
29 typedef struct nl_msg virNetlinkMsg;
30 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetlinkMsg, nlmsg_free);
31 
32 struct nl_msg *
33 virNetlinkMsgNew(int nlmsgtype,
34                  int nlmsgflags);
35 
36 #else
37 
38 struct nl_msg;
39 struct sockaddr_nl;
40 struct nlattr;
41 struct nlmsghdr;
42 
43 #endif /* WITH_LIBNL */
44 
45 int virNetlinkStartup(void);
46 void virNetlinkShutdown(void);
47 
48 int virNetlinkCommand(struct nl_msg *nl_msg,
49                       struct nlmsghdr **resp, unsigned int *respbuflen,
50                       uint32_t src_pid, uint32_t dst_pid,
51                       unsigned int protocol, unsigned int groups);
52 
53 typedef int (*virNetlinkDumpCallback)(struct nlmsghdr *resp,
54                                       void *data);
55 
56 int virNetlinkDumpCommand(struct nl_msg *nl_msg,
57                           virNetlinkDumpCallback callback,
58                           uint32_t src_pid, uint32_t dst_pid,
59                           unsigned int protocol, unsigned int groups,
60                           void *opaque);
61 
62 typedef struct _virNetlinkNewLinkData virNetlinkNewLinkData;
63 struct _virNetlinkNewLinkData {
64     const int *ifindex;             /* The index for the 'link' device */
65     const virMacAddr *mac;          /* The MAC address of the device */
66     const uint32_t *macvlan_mode;   /* The mode of macvlan */
67     const char *veth_peer;          /* The peer name for veth */
68 };
69 
70 int virNetlinkNewLink(const char *ifname,
71                       const char *type,
72                       virNetlinkNewLinkData *data,
73                       int *error);
74 
75 typedef int (*virNetlinkTalkFallback)(const char *ifname);
76 
77 int virNetlinkDelLink(const char *ifname, virNetlinkTalkFallback fallback);
78 
79 int virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen);
80 
81 int virNetlinkDumpLink(const char *ifname, int ifindex,
82                        void **nlData, struct nlattr **tb,
83                        uint32_t src_pid, uint32_t dst_pid)
84     G_GNUC_WARN_UNUSED_RESULT;
85 int
86 virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid);
87 
88 typedef void (*virNetlinkEventHandleCallback)(struct nlmsghdr *,
89                                               unsigned int length,
90                                               struct sockaddr_nl *peer,
91                                               bool *handled,
92                                               void *opaque);
93 
94 typedef void (*virNetlinkEventRemoveCallback)(int watch,
95                                               const virMacAddr *macaddr,
96                                               void *opaque);
97 
98 /**
99  * stopNetlinkEventServer: stop the monitor to receive netlink messages for libvirtd
100  */
101 int virNetlinkEventServiceStop(unsigned int protocol);
102 
103 /**
104  * stopNetlinkEventServerAll: stop all the monitors to receive netlink messages for libvirtd
105  */
106 int virNetlinkEventServiceStopAll(void);
107 
108 /**
109  * startNetlinkEventServer: start a monitor to receive netlink messages for libvirtd
110  */
111 int virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups);
112 
113 /**
114  * virNetlinkEventServiceIsRunning: returns if the netlink event service is running.
115  */
116 bool virNetlinkEventServiceIsRunning(unsigned int protocol);
117 
118 /**
119  * virNetlinkEventServiceLocalPid: returns nl_pid used to bind() netlink socket
120  */
121 int virNetlinkEventServiceLocalPid(unsigned int protocol);
122 
123 /**
124  * virNetlinkEventAddClient: register a callback for handling of netlink messages
125  */
126 int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
127                              virNetlinkEventRemoveCallback removeCB,
128                              void *opaque, const virMacAddr *macaddr,
129                              unsigned int protocol);
130 
131 /**
132  * virNetlinkEventRemoveClient: unregister a callback from a netlink monitor
133  */
134 int virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
135                                 unsigned int protocol);
136