| 1 | /* |
| 2 | * Copyright (c) 2015 FUJITSU LIMITED |
| 3 | * Author: Yang Hongyang <yanghy@cn.fujitsu.com> |
| 4 | * |
| 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. |
| 7 | */ |
| 8 | |
| 9 | #include "qemu/osdep.h" |
| 10 | #include "qapi/error.h" |
| 11 | #include "qapi/qmp/qerror.h" |
| 12 | #include "qemu/error-report.h" |
| 13 | |
| 14 | #include "net/filter.h" |
| 15 | #include "net/net.h" |
| 16 | #include "net/vhost_net.h" |
| 17 | #include "qom/object_interfaces.h" |
| 18 | #include "qemu/iov.h" |
| 19 | #include "qemu/module.h" |
| 20 | #include "net/colo.h" |
| 21 | #include "migration/colo.h" |
| 22 | |
| 23 | static inline bool qemu_can_skip_netfilter(NetFilterState *nf) |
| 24 | { |
| 25 | return !nf->on; |
| 26 | } |
| 27 | |
| 28 | ssize_t qemu_netfilter_receive(NetFilterState *nf, |
| 29 | NetFilterDirection direction, |
| 30 | NetClientState *sender, |
| 31 | unsigned flags, |
| 32 | const struct iovec *iov, |
| 33 | int iovcnt, |
| 34 | NetPacketSent *sent_cb) |
| 35 | { |
| 36 | if (qemu_can_skip_netfilter(nf)) { |
| 37 | return 0; |
| 38 | } |
| 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); |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static NetFilterState *netfilter_next(NetFilterState *nf, |
| 49 | NetFilterDirection dir) |
| 50 | { |
| 51 | NetFilterState *next; |
| 52 | |
| 53 | if (dir == NET_FILTER_DIRECTION_TX) { |
| 54 | /* forward walk through filters */ |
| 55 | next = QTAILQ_NEXT(nf, next); |
| 56 | } else { |
| 57 | /* reverse order */ |
| 58 | next = QTAILQ_PREV(nf, next); |
| 59 | } |
| 60 | |
| 61 | return next; |
| 62 | } |
| 63 | |
| 64 | ssize_t qemu_netfilter_pass_to_next(NetClientState *sender, |
| 65 | unsigned flags, |
| 66 | const struct iovec *iov, |
| 67 | int iovcnt, |
| 68 | void *opaque) |
| 69 | { |
| 70 | int ret = 0; |
| 71 | int direction; |
| 72 | NetFilterState *nf = opaque; |
| 73 | NetFilterState *next = NULL; |
| 74 | |
| 75 | if (!sender || !sender->peer) { |
| 76 | /* no receiver, or sender been deleted, no need to pass it further */ |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 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; |
| 84 | } else { |
| 85 | direction = NET_FILTER_DIRECTION_RX; |
| 86 | } |
| 87 | } else { |
| 88 | direction = nf->direction; |
| 89 | } |
| 90 | |
| 91 | next = netfilter_next(nf, direction); |
| 92 | while (next) { |
| 93 | /* |
| 94 | * if qemu_netfilter_pass_to_next has been called, it means that |
| 95 | * the packet was held by a filter and has already returned size |
| 96 | * to the sender, so sent_cb shouldn't be called later, just |
| 97 | * pass NULL to next. |
| 98 | */ |
| 99 | ret = qemu_netfilter_receive(next, direction, sender, flags, iov, |
| 100 | iovcnt, NULL); |
| 101 | if (ret) { |
| 102 | return ret; |
| 103 | } |
| 104 | next = netfilter_next(next, direction); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * We have gone through all filters, pass it to receiver. |
| 109 | * Do the valid check again in case sender or receiver been |
| 110 | * deleted while we go through filters. |
| 111 | */ |
| 112 | if (sender && sender->peer) { |
| 113 | qemu_net_queue_send_iov(sender->peer->incoming_queue, |
| 114 | sender, flags, iov, iovcnt, NULL); |
| 115 | } |
| 116 | |
| 117 | out: |
| 118 | /* no receiver, or sender been deleted */ |
| 119 | return iov_size(iov, iovcnt); |
| 120 | } |
| 121 | |
| 122 | static char *netfilter_get_netdev_id(Object *obj, Error **errp) |
| 123 | { |
| 124 | NetFilterState *nf = NETFILTER(obj); |
| 125 | |
| 126 | return g_strdup(nf->netdev_id); |
| 127 | } |
| 128 | |
| 129 | static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp) |
| 130 | { |
| 131 | NetFilterState *nf = NETFILTER(obj); |
| 132 | |
| 133 | g_free(nf->netdev_id); |
| 134 | nf->netdev_id = g_strdup(str); |
| 135 | } |
| 136 | |
| 137 | static int netfilter_get_direction(Object *obj, Error **errp G_GNUC_UNUSED) |
| 138 | { |
| 139 | NetFilterState *nf = NETFILTER(obj); |
| 140 | return nf->direction; |
| 141 | } |
| 142 | |
| 143 | static void netfilter_set_direction(Object *obj, int direction, Error **errp) |
| 144 | { |
| 145 | NetFilterState *nf = NETFILTER(obj); |
| 146 | nf->direction = direction; |
| 147 | } |
| 148 | |
| 149 | static char *netfilter_get_status(Object *obj, Error **errp) |
| 150 | { |
| 151 | NetFilterState *nf = NETFILTER(obj); |
| 152 | |
| 153 | return nf->on ? g_strdup("on") : g_strdup("off"); |
| 154 | } |
| 155 | |
| 156 | static void netfilter_set_status(Object *obj, const char *str, Error **errp) |
| 157 | { |
| 158 | NetFilterState *nf = NETFILTER(obj); |
| 159 | NetFilterClass *nfc = NETFILTER_GET_CLASS(obj); |
| 160 | |
| 161 | if (strcmp(str, "on") && strcmp(str, "off")) { |
| 162 | error_setg(errp, "Invalid value for netfilter status, " |
| 163 | "should be 'on' or 'off'"); |
| 164 | return; |
| 165 | } |
| 166 | if (nf->on == !strcmp(str, "on")) { |
| 167 | return; |
| 168 | } |
| 169 | nf->on = !nf->on; |
| 170 | if (nf->netdev && nfc->status_changed) { |
| 171 | nfc->status_changed(nf, errp); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static char *netfilter_get_position(Object *obj, Error **errp) |
| 176 | { |
| 177 | NetFilterState *nf = NETFILTER(obj); |
| 178 | |
| 179 | return g_strdup(nf->position); |
| 180 | } |
| 181 | |
| 182 | static void netfilter_set_position(Object *obj, const char *str, Error **errp) |
| 183 | { |
| 184 | NetFilterState *nf = NETFILTER(obj); |
| 185 | |
| 186 | g_free(nf->position); |
| 187 | nf->position = g_strdup(str); |
| 188 | } |
| 189 | |
| 190 | static char *netfilter_get_insert(Object *obj, Error **errp) |
| 191 | { |
| 192 | NetFilterState *nf = NETFILTER(obj); |
| 193 | |
| 194 | return nf->insert_before_flag ? g_strdup("before") : g_strdup("behind"); |
| 195 | } |
| 196 | |
| 197 | static void netfilter_set_insert(Object *obj, const char *str, Error **errp) |
| 198 | { |
| 199 | NetFilterState *nf = NETFILTER(obj); |
| 200 | |
| 201 | if (strcmp(str, "before") && strcmp(str, "behind")) { |
| 202 | error_setg(errp, "Invalid value for netfilter insert, " |
| 203 | "should be 'before' or 'behind'"); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | nf->insert_before_flag = !strcmp(str, "before"); |
| 208 | } |
| 209 | |
| 210 | static void netfilter_init(Object *obj) |
| 211 | { |
| 212 | NetFilterState *nf = NETFILTER(obj); |
| 213 | |
| 214 | nf->on = true; |
| 215 | nf->insert_before_flag = false; |
| 216 | nf->position = g_strdup("tail"); |
| 217 | } |
| 218 | |
| 219 | static void netfilter_complete(UserCreatable *uc, Error **errp) |
| 220 | { |
| 221 | NetFilterState *nf = NETFILTER(uc); |
| 222 | NetFilterState *position = NULL; |
| 223 | NetClientState *ncs[MAX_QUEUE_NUM]; |
| 224 | NetFilterClass *nfc = NETFILTER_GET_CLASS(uc); |
| 225 | int queues; |
| 226 | Error *local_err = NULL; |
| 227 | |
| 228 | if (!nf->netdev_id) { |
| 229 | error_setg(errp, "Parameter 'netdev' is required"); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | queues = qemu_find_net_clients_except(nf->netdev_id, ncs, |
| 234 | NET_CLIENT_DRIVER_NIC, |
| 235 | MAX_QUEUE_NUM); |
| 236 | if (queues < 1) { |
| 237 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev", |
| 238 | "a network backend id"); |
| 239 | return; |
| 240 | } else if (queues > 1) { |
| 241 | error_setg(errp, "multiqueue is not supported"); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (get_vhost_net(ncs[0])) { |
| 246 | error_setg(errp, "Vhost is not supported"); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | if (strcmp(nf->position, "head") && strcmp(nf->position, "tail")) { |
| 251 | Object *container; |
| 252 | Object *obj; |
| 253 | char *position_id; |
| 254 | |
| 255 | if (!g_str_has_prefix(nf->position, "id=")) { |
| 256 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "position", |
| 257 | "'head', 'tail' or 'id=<id>'"); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | /* get the id from the string */ |
| 262 | position_id = g_strndup(nf->position + 3, strlen(nf->position) - 3); |
| 263 | |
| 264 | /* Search for the position to insert before/behind */ |
| 265 | container = object_get_objects_root(); |
| 266 | obj = object_resolve_path_component(container, position_id); |
| 267 | if (!obj) { |
| 268 | error_setg(errp, "filter '%s' not found", position_id); |
| 269 | g_free(position_id); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | position = NETFILTER(obj); |
| 274 | |
| 275 | if (position->netdev != ncs[0]) { |
| 276 | error_setg(errp, "filter '%s' belongs to a different netdev", |
| 277 | position_id); |
| 278 | g_free(position_id); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | g_free(position_id); |
| 283 | } |
| 284 | |
| 285 | nf->netdev = ncs[0]; |
| 286 | |
| 287 | if (nfc->setup) { |
| 288 | nfc->setup(nf, &local_err); |
| 289 | if (local_err) { |
| 290 | error_propagate(errp, local_err); |
| 291 | return; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (position) { |
| 296 | if (nf->insert_before_flag) { |
| 297 | QTAILQ_INSERT_BEFORE(position, nf, next); |
| 298 | } else { |
| 299 | QTAILQ_INSERT_AFTER(&nf->netdev->filters, position, nf, next); |
| 300 | } |
| 301 | } else if (!strcmp(nf->position, "head")) { |
| 302 | QTAILQ_INSERT_HEAD(&nf->netdev->filters, nf, next); |
| 303 | } else if (!strcmp(nf->position, "tail")) { |
| 304 | QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void netfilter_finalize(Object *obj) |
| 309 | { |
| 310 | NetFilterState *nf = NETFILTER(obj); |
| 311 | NetFilterClass *nfc = NETFILTER_GET_CLASS(obj); |
| 312 | |
| 313 | if (nfc->cleanup) { |
| 314 | nfc->cleanup(nf); |
| 315 | } |
| 316 | |
| 317 | if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters) && |
| 318 | QTAILQ_IN_USE(nf, next)) { |
| 319 | QTAILQ_REMOVE(&nf->netdev->filters, nf, next); |
| 320 | } |
| 321 | g_free(nf->netdev_id); |
| 322 | g_free(nf->position); |
| 323 | } |
| 324 | |
| 325 | static void default_handle_event(NetFilterState *nf, int event, Error **errp) |
| 326 | { |
| 327 | switch (event) { |
| 328 | case COLO_EVENT_CHECKPOINT: |
| 329 | break; |
| 330 | case COLO_EVENT_FAILOVER: |
| 331 | object_property_set_str(OBJECT(nf), "status", "off", errp); |
| 332 | break; |
| 333 | default: |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static void netfilter_class_init(ObjectClass *oc, const void *data) |
| 339 | { |
| 340 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); |
| 341 | NetFilterClass *nfc = NETFILTER_CLASS(oc); |
| 342 | |
| 343 | object_class_property_add_str(oc, "netdev", |
| 344 | netfilter_get_netdev_id, netfilter_set_netdev_id); |
| 345 | object_class_property_add_enum(oc, "queue", "NetFilterDirection", |
| 346 | &NetFilterDirection_lookup, |
| 347 | netfilter_get_direction, netfilter_set_direction); |
| 348 | object_class_property_add_str(oc, "status", |
| 349 | netfilter_get_status, netfilter_set_status); |
| 350 | object_class_property_add_str(oc, "position", |
| 351 | netfilter_get_position, netfilter_set_position); |
| 352 | object_class_property_add_str(oc, "insert", |
| 353 | netfilter_get_insert, netfilter_set_insert); |
| 354 | |
| 355 | ucc->complete = netfilter_complete; |
| 356 | nfc->handle_event = default_handle_event; |
| 357 | } |
| 358 | |
| 359 | static const TypeInfo netfilter_info = { |
| 360 | .name = TYPE_NETFILTER, |
| 361 | .parent = TYPE_OBJECT, |
| 362 | .abstract = true, |
| 363 | .class_size = sizeof(NetFilterClass), |
| 364 | .class_init = netfilter_class_init, |
| 365 | .instance_size = sizeof(NetFilterState), |
| 366 | .instance_init = netfilter_init, |
| 367 | .instance_finalize = netfilter_finalize, |
| 368 | .interfaces = (const InterfaceInfo[]) { |
| 369 | { TYPE_USER_CREATABLE }, |
| 370 | { } |
| 371 | } |
| 372 | }; |
| 373 | |
| 374 | static void register_types(void) |
| 375 | { |
| 376 | type_register_static(&netfilter_info); |
| 377 | } |
| 378 | |
| 379 | type_init(register_types); |