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-common.h"
10 #include "qapi/qmp/qerror.h"
11 #include "qemu/error-report.h"
13 #include "net/filter.h"
15 #include "net/vhost_net.h"
16 #include "qom/object_interfaces.h"
18 #include "qapi/string-output-visitor.h"
20 ssize_t
qemu_netfilter_receive(NetFilterState
*nf
,
21 NetFilterDirection direction
,
22 NetClientState
*sender
,
24 const struct iovec
*iov
,
26 NetPacketSent
*sent_cb
)
28 if (nf
->direction
== direction
||
29 nf
->direction
== NET_FILTER_DIRECTION_ALL
) {
30 return NETFILTER_GET_CLASS(OBJECT(nf
))->receive_iov(
31 nf
, sender
, flags
, iov
, iovcnt
, sent_cb
);
37 static NetFilterState
*netfilter_next(NetFilterState
*nf
,
38 NetFilterDirection dir
)
42 if (dir
== NET_FILTER_DIRECTION_TX
) {
43 /* forward walk through filters */
44 next
= QTAILQ_NEXT(nf
, next
);
47 next
= QTAILQ_PREV(nf
, NetFilterHead
, next
);
53 ssize_t
qemu_netfilter_pass_to_next(NetClientState
*sender
,
55 const struct iovec
*iov
,
61 NetFilterState
*nf
= opaque
;
62 NetFilterState
*next
= NULL
;
64 if (!sender
|| !sender
->peer
) {
65 /* no receiver, or sender been deleted, no need to pass it further */
69 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
) {
70 if (sender
== nf
->netdev
) {
71 /* This packet is sent by netdev itself */
72 direction
= NET_FILTER_DIRECTION_TX
;
74 direction
= NET_FILTER_DIRECTION_RX
;
77 direction
= nf
->direction
;
80 next
= netfilter_next(nf
, direction
);
83 * if qemu_netfilter_pass_to_next been called, means that
84 * the packet has been hold by filter and has already retured size
85 * to the sender, so sent_cb shouldn't be called later, just
88 ret
= qemu_netfilter_receive(next
, direction
, sender
, flags
, iov
,
93 next
= netfilter_next(next
, direction
);
97 * We have gone through all filters, pass it to receiver.
98 * Do the valid check again incase sender or receiver been
99 * deleted while we go through filters.
101 if (sender
&& sender
->peer
) {
102 qemu_net_queue_send_iov(sender
->peer
->incoming_queue
,
103 sender
, flags
, iov
, iovcnt
, NULL
);
107 /* no receiver, or sender been deleted */
108 return iov_size(iov
, iovcnt
);
111 static char *netfilter_get_netdev_id(Object
*obj
, Error
**errp
)
113 NetFilterState
*nf
= NETFILTER(obj
);
115 return g_strdup(nf
->netdev_id
);
118 static void netfilter_set_netdev_id(Object
*obj
, const char *str
, Error
**errp
)
120 NetFilterState
*nf
= NETFILTER(obj
);
122 nf
->netdev_id
= g_strdup(str
);
125 static int netfilter_get_direction(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
127 NetFilterState
*nf
= NETFILTER(obj
);
128 return nf
->direction
;
131 static void netfilter_set_direction(Object
*obj
, int direction
, Error
**errp
)
133 NetFilterState
*nf
= NETFILTER(obj
);
134 nf
->direction
= direction
;
137 static void netfilter_init(Object
*obj
)
139 object_property_add_str(obj
, "netdev",
140 netfilter_get_netdev_id
, netfilter_set_netdev_id
,
142 object_property_add_enum(obj
, "queue", "NetFilterDirection",
143 NetFilterDirection_lookup
,
144 netfilter_get_direction
, netfilter_set_direction
,
148 static void netfilter_complete(UserCreatable
*uc
, Error
**errp
)
150 NetFilterState
*nf
= NETFILTER(uc
);
151 NetClientState
*ncs
[MAX_QUEUE_NUM
];
152 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(uc
);
154 Error
*local_err
= NULL
;
156 ObjectProperty
*prop
;
157 ObjectPropertyIterator iter
;
158 StringOutputVisitor
*ov
;
160 if (!nf
->netdev_id
) {
161 error_setg(errp
, "Parameter 'netdev' is required");
165 queues
= qemu_find_net_clients_except(nf
->netdev_id
, ncs
,
166 NET_CLIENT_OPTIONS_KIND_NIC
,
169 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "netdev",
170 "a network backend id");
172 } else if (queues
> 1) {
173 error_setg(errp
, "multiqueue is not supported");
177 if (get_vhost_net(ncs
[0])) {
178 error_setg(errp
, "Vhost is not supported");
185 nfc
->setup(nf
, &local_err
);
187 error_propagate(errp
, local_err
);
191 QTAILQ_INSERT_TAIL(&nf
->netdev
->filters
, nf
, next
);
193 /* generate info str */
194 object_property_iter_init(&iter
, OBJECT(nf
));
195 while ((prop
= object_property_iter_next(&iter
))) {
196 if (!strcmp(prop
->name
, "type")) {
199 ov
= string_output_visitor_new(false);
200 object_property_get(OBJECT(nf
), string_output_get_visitor(ov
),
202 str
= string_output_get_string(ov
);
203 string_output_visitor_cleanup(ov
);
204 info
= g_strdup_printf(",%s=%s", prop
->name
, str
);
205 g_strlcat(nf
->info_str
, info
, sizeof(nf
->info_str
));
211 static void netfilter_finalize(Object
*obj
)
213 NetFilterState
*nf
= NETFILTER(obj
);
214 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(obj
);
220 if (nf
->netdev
&& !QTAILQ_EMPTY(&nf
->netdev
->filters
)) {
221 QTAILQ_REMOVE(&nf
->netdev
->filters
, nf
, next
);
223 g_free(nf
->netdev_id
);
226 static void netfilter_class_init(ObjectClass
*oc
, void *data
)
228 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
230 ucc
->complete
= netfilter_complete
;
233 static const TypeInfo netfilter_info
= {
234 .name
= TYPE_NETFILTER
,
235 .parent
= TYPE_OBJECT
,
237 .class_size
= sizeof(NetFilterClass
),
238 .class_init
= netfilter_class_init
,
239 .instance_size
= sizeof(NetFilterState
),
240 .instance_init
= netfilter_init
,
241 .instance_finalize
= netfilter_finalize
,
242 .interfaces
= (InterfaceInfo
[]) {
243 { TYPE_USER_CREATABLE
},
248 static void register_types(void)
250 type_register_static(&netfilter_info
);
253 type_init(register_types
);