2 * Copyright (c) 2015 FUJITSU LIMITED
3 * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
5 * This work is licensed under the terms of the GNU GPL, version 2 or
6 * later. See the COPYING file in the top-level directory.
9 #include "qemu/osdep.h"
10 #include "qapi/error.h"
11 #include "qemu-common.h"
12 #include "qapi/qmp/qerror.h"
13 #include "qemu/error-report.h"
15 #include "net/filter.h"
17 #include "net/vhost_net.h"
18 #include "qom/object_interfaces.h"
21 #include "migration/colo.h"
23 static inline bool qemu_can_skip_netfilter(NetFilterState *nf)
28 ssize_t qemu_netfilter_receive(NetFilterState *nf,
29 NetFilterDirection direction,
30 NetClientState *sender,
32 const struct iovec *iov,
34 NetPacketSent *sent_cb)
36 if (qemu_can_skip_netfilter(nf)) {
39 if (nf->direction == direction ||
40 nf->direction == NET_FILTER_DIRECTION_ALL) {
41 return NETFILTER_GET_CLASS(OBJECT(nf))->receive_iov(
42 nf, sender, flags, iov, iovcnt, sent_cb);
48 static NetFilterState *netfilter_next(NetFilterState *nf,
49 NetFilterDirection dir)
53 if (dir == NET_FILTER_DIRECTION_TX) {
54 /* forward walk through filters */
55 next = QTAILQ_NEXT(nf, next);
58 next = QTAILQ_PREV(nf, NetFilterHead, next);
64 ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
66 const struct iovec *iov,
72 NetFilterState *nf = opaque;
73 NetFilterState *next = NULL;
75 if (!sender || !sender->peer) {
76 /* no receiver, or sender been deleted, no need to pass it further */
80 if (nf->direction == NET_FILTER_DIRECTION_ALL) {
81 if (sender == nf->netdev) {
82 /* This packet is sent by netdev itself */
83 direction = NET_FILTER_DIRECTION_TX;
85 direction = NET_FILTER_DIRECTION_RX;
88 direction = nf->direction;
91 next = netfilter_next(nf, direction);
94 * if qemu_netfilter_pass_to_next been called, means that
95 * the packet has been hold by filter and has already retured size
96 * to the sender, so sent_cb shouldn't be called later, just
99 ret = qemu_netfilter_receive(next, direction, sender, flags, iov,
104 next = netfilter_next(next, direction);
108 * We have gone through all filters, pass it to receiver.
109 * Do the valid check again incase sender or receiver been
110 * deleted while we go through filters.
112 if (sender && sender->peer) {
113 qemu_net_queue_send_iov(sender->peer->incoming_queue,
114 sender, flags, iov, iovcnt, NULL);
118 /* no receiver, or sender been deleted */
119 return iov_size(iov, iovcnt);
122 static char *netfilter_get_netdev_id(Object *obj, Error **errp)
124 NetFilterState *nf = NETFILTER(obj);
126 return g_strdup(nf->netdev_id);
129 static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
131 NetFilterState *nf = NETFILTER(obj);
133 nf->netdev_id = g_strdup(str);
136 static int netfilter_get_direction(Object *obj, Error **errp G_GNUC_UNUSED)
138 NetFilterState *nf = NETFILTER(obj);
139 return nf->direction;
142 static void netfilter_set_direction(Object *obj, int direction, Error **errp)
144 NetFilterState *nf = NETFILTER(obj);
145 nf->direction = direction;
148 static char *netfilter_get_status(Object *obj, Error **errp)
150 NetFilterState *nf = NETFILTER(obj);
152 return nf->on ? g_strdup("on") : g_strdup("off");
155 static void netfilter_set_status(Object *obj, const char *str, Error **errp)
157 NetFilterState *nf = NETFILTER(obj);
158 NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
160 if (strcmp(str, "on") && strcmp(str, "off")) {
161 error_setg(errp, "Invalid value for netfilter status, "
162 "should be 'on' or 'off'");
165 if (nf->on == !strcmp(str, "on")) {
169 if (nf->netdev && nfc->status_changed) {
170 nfc->status_changed(nf, errp);
174 static void netfilter_init(Object *obj)
176 NetFilterState *nf = NETFILTER(obj);
180 object_property_add_str(obj, "netdev",
181 netfilter_get_netdev_id, netfilter_set_netdev_id,
183 object_property_add_enum(obj, "queue", "NetFilterDirection",
184 &NetFilterDirection_lookup,
185 netfilter_get_direction, netfilter_set_direction,
187 object_property_add_str(obj, "status",
188 netfilter_get_status, netfilter_set_status,
192 static void netfilter_complete(UserCreatable *uc, Error **errp)
194 NetFilterState *nf = NETFILTER(uc);
195 NetClientState *ncs[MAX_QUEUE_NUM];
196 NetFilterClass *nfc = NETFILTER_GET_CLASS(uc);
198 Error *local_err = NULL;
200 if (!nf->netdev_id) {
201 error_setg(errp, "Parameter 'netdev' is required");
205 queues = qemu_find_net_clients_except(nf->netdev_id, ncs,
206 NET_CLIENT_DRIVER_NIC,
209 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev",
210 "a network backend id");
212 } else if (queues > 1) {
213 error_setg(errp, "multiqueue is not supported");
217 if (get_vhost_net(ncs[0])) {
218 error_setg(errp, "Vhost is not supported");
225 nfc->setup(nf, &local_err);
227 error_propagate(errp, local_err);
231 QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
234 static void netfilter_finalize(Object *obj)
236 NetFilterState *nf = NETFILTER(obj);
237 NetFilterClass *nfc = NETFILTER_GET_CLASS(obj);
243 if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters) &&
244 QTAILQ_IN_USE(nf, next)) {
245 QTAILQ_REMOVE(&nf->netdev->filters, nf, next);
247 g_free(nf->netdev_id);
250 static void default_handle_event(NetFilterState *nf, int event, Error **errp)
253 case COLO_EVENT_CHECKPOINT:
255 case COLO_EVENT_FAILOVER:
256 object_property_set_str(OBJECT(nf), "off", "status", errp);
263 static void netfilter_class_init(ObjectClass *oc, void *data)
265 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
266 NetFilterClass *nfc = NETFILTER_CLASS(oc);
268 ucc->complete = netfilter_complete;
269 nfc->handle_event = default_handle_event;
272 static const TypeInfo netfilter_info = {
273 .name = TYPE_NETFILTER,
274 .parent = TYPE_OBJECT,
276 .class_size = sizeof(NetFilterClass),
277 .class_init = netfilter_class_init,
278 .instance_size = sizeof(NetFilterState),
279 .instance_init = netfilter_init,
280 .instance_finalize = netfilter_finalize,
281 .interfaces = (InterfaceInfo[]) {
282 { TYPE_USER_CREATABLE },
287 static void register_types(void)
289 type_register_static(&netfilter_info);
292 type_init(register_types);