Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. |
| 3 | * Copyright (c) 2016 FUJITSU LIMITED |
| 4 | * Copyright (c) 2016 Intel Corporation |
| 5 | * |
| 6 | * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com> |
| 7 | * |
| 8 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 9 | * later. See the COPYING file in the top-level directory. |
| 10 | */ |
| 11 | |
| 12 | #include "qemu/osdep.h" |
| 13 | #include "net/filter.h" |
| 14 | #include "net/net.h" |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 15 | #include "qapi/error.h" |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 16 | #include "qom/object.h" |
| 17 | #include "qemu/main-loop.h" |
| 18 | #include "qemu/error-report.h" |
| 19 | #include "trace.h" |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 20 | #include "chardev/char-fe.h" |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 21 | #include "qemu/iov.h" |
| 22 | #include "qemu/sockets.h" |
| 23 | |
| 24 | #define FILTER_MIRROR(obj) \ |
| 25 | OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR) |
| 26 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 27 | #define FILTER_REDIRECTOR(obj) \ |
| 28 | OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_REDIRECTOR) |
| 29 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 30 | #define TYPE_FILTER_MIRROR "filter-mirror" |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 31 | #define TYPE_FILTER_REDIRECTOR "filter-redirector" |
| 32 | #define REDIRECTOR_MAX_LEN NET_BUFSIZE |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 33 | |
| 34 | typedef struct MirrorState { |
| 35 | NetFilterState parent_obj; |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 36 | char *indev; |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 37 | char *outdev; |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 38 | CharBackend chr_in; |
| 39 | CharBackend chr_out; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 40 | SocketReadState rs; |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 41 | bool vnet_hdr; |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 42 | } MirrorState; |
| 43 | |
Zhang Chen | dc3c5ac | 2017-07-04 14:53:47 +0800 | [diff] [blame] | 44 | static int filter_send(MirrorState *s, |
Zhang Chen | e05dc4c | 2017-05-17 10:09:39 +0800 | [diff] [blame] | 45 | const struct iovec *iov, |
| 46 | int iovcnt) |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 47 | { |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 48 | NetFilterState *nf = NETFILTER(s); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 49 | int ret = 0; |
| 50 | ssize_t size = 0; |
Zhang Chen | f0aabd5 | 2017-03-02 11:59:30 +0800 | [diff] [blame] | 51 | uint32_t len = 0; |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 52 | char *buf; |
| 53 | |
| 54 | size = iov_size(iov, iovcnt); |
| 55 | if (!size) { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | len = htonl(size); |
Zhang Chen | dc3c5ac | 2017-07-04 14:53:47 +0800 | [diff] [blame] | 60 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len)); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 61 | if (ret != sizeof(len)) { |
| 62 | goto err; |
| 63 | } |
| 64 | |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 65 | if (s->vnet_hdr) { |
| 66 | /* |
| 67 | * If vnet_hdr = on, we send vnet header len to make other |
| 68 | * module(like colo-compare) know how to parse net |
| 69 | * packet correctly. |
| 70 | */ |
| 71 | ssize_t vnet_hdr_len; |
| 72 | |
| 73 | vnet_hdr_len = nf->netdev->vnet_hdr_len; |
| 74 | |
| 75 | len = htonl(vnet_hdr_len); |
| 76 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len)); |
| 77 | if (ret != sizeof(len)) { |
| 78 | goto err; |
| 79 | } |
| 80 | } |
| 81 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 82 | buf = g_malloc(size); |
| 83 | iov_to_buf(iov, iovcnt, 0, buf, size); |
Zhang Chen | dc3c5ac | 2017-07-04 14:53:47 +0800 | [diff] [blame] | 84 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 85 | g_free(buf); |
| 86 | if (ret != size) { |
| 87 | goto err; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | |
| 92 | err: |
| 93 | return ret < 0 ? ret : -EIO; |
| 94 | } |
| 95 | |
Zhang Chen | f0aabd5 | 2017-03-02 11:59:30 +0800 | [diff] [blame] | 96 | static void redirector_to_filter(NetFilterState *nf, |
| 97 | const uint8_t *buf, |
| 98 | int len) |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 99 | { |
| 100 | struct iovec iov = { |
| 101 | .iov_base = (void *)buf, |
| 102 | .iov_len = len, |
| 103 | }; |
| 104 | |
| 105 | if (nf->direction == NET_FILTER_DIRECTION_ALL || |
| 106 | nf->direction == NET_FILTER_DIRECTION_TX) { |
| 107 | qemu_netfilter_pass_to_next(nf->netdev, 0, &iov, 1, nf); |
| 108 | } |
| 109 | |
| 110 | if (nf->direction == NET_FILTER_DIRECTION_ALL || |
| 111 | nf->direction == NET_FILTER_DIRECTION_RX) { |
| 112 | qemu_netfilter_pass_to_next(nf->netdev->peer, 0, &iov, 1, nf); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static int redirector_chr_can_read(void *opaque) |
| 117 | { |
| 118 | return REDIRECTOR_MAX_LEN; |
| 119 | } |
| 120 | |
| 121 | static void redirector_chr_read(void *opaque, const uint8_t *buf, int size) |
| 122 | { |
| 123 | NetFilterState *nf = opaque; |
| 124 | MirrorState *s = FILTER_REDIRECTOR(nf); |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 125 | int ret; |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 126 | |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 127 | ret = net_fill_rstate(&s->rs, buf, size); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 128 | |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 129 | if (ret == -1) { |
Marc-André Lureau | 39ab61c | 2016-10-22 12:53:03 +0300 | [diff] [blame] | 130 | qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 131 | NULL, NULL, NULL, true); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | static void redirector_chr_event(void *opaque, int event) |
| 136 | { |
| 137 | NetFilterState *nf = opaque; |
| 138 | MirrorState *s = FILTER_REDIRECTOR(nf); |
| 139 | |
| 140 | switch (event) { |
| 141 | case CHR_EVENT_CLOSED: |
Marc-André Lureau | 39ab61c | 2016-10-22 12:53:03 +0300 | [diff] [blame] | 142 | qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 143 | NULL, NULL, NULL, true); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 150 | static ssize_t filter_mirror_receive_iov(NetFilterState *nf, |
| 151 | NetClientState *sender, |
| 152 | unsigned flags, |
| 153 | const struct iovec *iov, |
| 154 | int iovcnt, |
| 155 | NetPacketSent *sent_cb) |
| 156 | { |
Zhang Chen | ba8940d | 2016-03-15 18:02:51 +0800 | [diff] [blame] | 157 | MirrorState *s = FILTER_MIRROR(nf); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 158 | int ret; |
| 159 | |
Zhang Chen | dc3c5ac | 2017-07-04 14:53:47 +0800 | [diff] [blame] | 160 | ret = filter_send(s, iov, iovcnt); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 161 | if (ret) { |
Zhang Chen | e05dc4c | 2017-05-17 10:09:39 +0800 | [diff] [blame] | 162 | error_report("filter mirror send failed(%s)", strerror(-ret)); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * we don't hope this error interrupt the normal |
| 167 | * path of net packet, so we always return zero. |
| 168 | */ |
| 169 | return 0; |
| 170 | } |
| 171 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 172 | static ssize_t filter_redirector_receive_iov(NetFilterState *nf, |
| 173 | NetClientState *sender, |
| 174 | unsigned flags, |
| 175 | const struct iovec *iov, |
| 176 | int iovcnt, |
| 177 | NetPacketSent *sent_cb) |
| 178 | { |
| 179 | MirrorState *s = FILTER_REDIRECTOR(nf); |
| 180 | int ret; |
| 181 | |
Anton Nefedov | 3065070 | 2017-07-06 15:08:52 +0300 | [diff] [blame] | 182 | if (qemu_chr_fe_backend_connected(&s->chr_out)) { |
Zhang Chen | dc3c5ac | 2017-07-04 14:53:47 +0800 | [diff] [blame] | 183 | ret = filter_send(s, iov, iovcnt); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 184 | if (ret) { |
Zhang Chen | e05dc4c | 2017-05-17 10:09:39 +0800 | [diff] [blame] | 185 | error_report("filter redirector send failed(%s)", strerror(-ret)); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 186 | } |
| 187 | return iov_size(iov, iovcnt); |
| 188 | } else { |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 193 | static void filter_mirror_cleanup(NetFilterState *nf) |
| 194 | { |
| 195 | MirrorState *s = FILTER_MIRROR(nf); |
| 196 | |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 197 | qemu_chr_fe_deinit(&s->chr_out, false); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 200 | static void filter_redirector_cleanup(NetFilterState *nf) |
| 201 | { |
| 202 | MirrorState *s = FILTER_REDIRECTOR(nf); |
| 203 | |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 204 | qemu_chr_fe_deinit(&s->chr_in, false); |
| 205 | qemu_chr_fe_deinit(&s->chr_out, false); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 208 | static void filter_mirror_setup(NetFilterState *nf, Error **errp) |
| 209 | { |
| 210 | MirrorState *s = FILTER_MIRROR(nf); |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 211 | Chardev *chr; |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 212 | |
Eduardo Otubo | aa1530d | 2017-09-29 14:03:39 +0200 | [diff] [blame] | 213 | if (s->outdev == NULL) { |
| 214 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "filter-mirror parameter"\ |
| 215 | " 'outdev' cannot be empty"); |
| 216 | return; |
| 217 | } |
| 218 | |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 219 | chr = qemu_chr_find(s->outdev); |
| 220 | if (chr == NULL) { |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 221 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 222 | "Device '%s' not found", s->outdev); |
| 223 | return; |
| 224 | } |
| 225 | |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 226 | qemu_chr_fe_init(&s->chr_out, chr, errp); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 229 | static void redirector_rs_finalize(SocketReadState *rs) |
| 230 | { |
| 231 | MirrorState *s = container_of(rs, MirrorState, rs); |
| 232 | NetFilterState *nf = NETFILTER(s); |
| 233 | |
| 234 | redirector_to_filter(nf, rs->buf, rs->packet_len); |
| 235 | } |
| 236 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 237 | static void filter_redirector_setup(NetFilterState *nf, Error **errp) |
| 238 | { |
| 239 | MirrorState *s = FILTER_REDIRECTOR(nf); |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 240 | Chardev *chr; |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 241 | |
| 242 | if (!s->indev && !s->outdev) { |
| 243 | error_setg(errp, "filter redirector needs 'indev' or " |
| 244 | "'outdev' at least one property set"); |
| 245 | return; |
| 246 | } else if (s->indev && s->outdev) { |
| 247 | if (!strcmp(s->indev, s->outdev)) { |
| 248 | error_setg(errp, "'indev' and 'outdev' could not be same " |
| 249 | "for filter redirector"); |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 254 | net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 255 | |
| 256 | if (s->indev) { |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 257 | chr = qemu_chr_find(s->indev); |
| 258 | if (chr == NULL) { |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 259 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 260 | "IN Device '%s' not found", s->indev); |
| 261 | return; |
| 262 | } |
| 263 | |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 264 | if (!qemu_chr_fe_init(&s->chr_in, chr, errp)) { |
| 265 | return; |
| 266 | } |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 267 | |
| 268 | qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read, |
| 269 | redirector_chr_read, redirector_chr_event, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 270 | NULL, nf, NULL, true); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (s->outdev) { |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 274 | chr = qemu_chr_find(s->outdev); |
| 275 | if (chr == NULL) { |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 276 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 277 | "OUT Device '%s' not found", s->outdev); |
| 278 | return; |
| 279 | } |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 280 | if (!qemu_chr_fe_init(&s->chr_out, chr, errp)) { |
| 281 | return; |
| 282 | } |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 286 | static void filter_mirror_class_init(ObjectClass *oc, void *data) |
| 287 | { |
| 288 | NetFilterClass *nfc = NETFILTER_CLASS(oc); |
| 289 | |
| 290 | nfc->setup = filter_mirror_setup; |
| 291 | nfc->cleanup = filter_mirror_cleanup; |
| 292 | nfc->receive_iov = filter_mirror_receive_iov; |
| 293 | } |
| 294 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 295 | static void filter_redirector_class_init(ObjectClass *oc, void *data) |
| 296 | { |
| 297 | NetFilterClass *nfc = NETFILTER_CLASS(oc); |
| 298 | |
| 299 | nfc->setup = filter_redirector_setup; |
| 300 | nfc->cleanup = filter_redirector_cleanup; |
| 301 | nfc->receive_iov = filter_redirector_receive_iov; |
| 302 | } |
| 303 | |
| 304 | static char *filter_redirector_get_indev(Object *obj, Error **errp) |
| 305 | { |
| 306 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 307 | |
| 308 | return g_strdup(s->indev); |
| 309 | } |
| 310 | |
Zhang Chen | e05dc4c | 2017-05-17 10:09:39 +0800 | [diff] [blame] | 311 | static void filter_redirector_set_indev(Object *obj, |
| 312 | const char *value, |
| 313 | Error **errp) |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 314 | { |
| 315 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 316 | |
| 317 | g_free(s->indev); |
| 318 | s->indev = g_strdup(value); |
| 319 | } |
| 320 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 321 | static char *filter_mirror_get_outdev(Object *obj, Error **errp) |
| 322 | { |
| 323 | MirrorState *s = FILTER_MIRROR(obj); |
| 324 | |
| 325 | return g_strdup(s->outdev); |
| 326 | } |
| 327 | |
Zhang Chen | e05dc4c | 2017-05-17 10:09:39 +0800 | [diff] [blame] | 328 | static void filter_mirror_set_outdev(Object *obj, |
| 329 | const char *value, |
| 330 | Error **errp) |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 331 | { |
| 332 | MirrorState *s = FILTER_MIRROR(obj); |
| 333 | |
| 334 | g_free(s->outdev); |
| 335 | s->outdev = g_strdup(value); |
| 336 | if (!s->outdev) { |
Zhang Chen | 52cfcb4 | 2016-09-30 10:27:58 +0800 | [diff] [blame] | 337 | error_setg(errp, "filter mirror needs 'outdev' " |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 338 | "property set"); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | } |
| 342 | |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 343 | static bool filter_mirror_get_vnet_hdr(Object *obj, Error **errp) |
| 344 | { |
| 345 | MirrorState *s = FILTER_MIRROR(obj); |
| 346 | |
| 347 | return s->vnet_hdr; |
| 348 | } |
| 349 | |
| 350 | static void filter_mirror_set_vnet_hdr(Object *obj, bool value, Error **errp) |
| 351 | { |
| 352 | MirrorState *s = FILTER_MIRROR(obj); |
| 353 | |
| 354 | s->vnet_hdr = value; |
| 355 | } |
| 356 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 357 | static char *filter_redirector_get_outdev(Object *obj, Error **errp) |
| 358 | { |
| 359 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 360 | |
| 361 | return g_strdup(s->outdev); |
| 362 | } |
| 363 | |
Zhang Chen | e05dc4c | 2017-05-17 10:09:39 +0800 | [diff] [blame] | 364 | static void filter_redirector_set_outdev(Object *obj, |
| 365 | const char *value, |
| 366 | Error **errp) |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 367 | { |
| 368 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 369 | |
| 370 | g_free(s->outdev); |
| 371 | s->outdev = g_strdup(value); |
| 372 | } |
| 373 | |
Zhang Chen | 00d5c24 | 2017-07-04 14:53:49 +0800 | [diff] [blame] | 374 | static bool filter_redirector_get_vnet_hdr(Object *obj, Error **errp) |
| 375 | { |
| 376 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 377 | |
| 378 | return s->vnet_hdr; |
| 379 | } |
| 380 | |
| 381 | static void filter_redirector_set_vnet_hdr(Object *obj, |
| 382 | bool value, |
| 383 | Error **errp) |
| 384 | { |
| 385 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 386 | |
| 387 | s->vnet_hdr = value; |
| 388 | } |
| 389 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 390 | static void filter_mirror_init(Object *obj) |
| 391 | { |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 392 | MirrorState *s = FILTER_MIRROR(obj); |
| 393 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 394 | object_property_add_str(obj, "outdev", filter_mirror_get_outdev, |
| 395 | filter_mirror_set_outdev, NULL); |
Zhang Chen | e2521f0 | 2017-07-04 14:53:48 +0800 | [diff] [blame] | 396 | |
| 397 | s->vnet_hdr = false; |
| 398 | object_property_add_bool(obj, "vnet_hdr_support", |
| 399 | filter_mirror_get_vnet_hdr, |
| 400 | filter_mirror_set_vnet_hdr, NULL); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 403 | static void filter_redirector_init(Object *obj) |
| 404 | { |
Zhang Chen | 00d5c24 | 2017-07-04 14:53:49 +0800 | [diff] [blame] | 405 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 406 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 407 | object_property_add_str(obj, "indev", filter_redirector_get_indev, |
| 408 | filter_redirector_set_indev, NULL); |
| 409 | object_property_add_str(obj, "outdev", filter_redirector_get_outdev, |
| 410 | filter_redirector_set_outdev, NULL); |
Zhang Chen | 00d5c24 | 2017-07-04 14:53:49 +0800 | [diff] [blame] | 411 | |
| 412 | s->vnet_hdr = false; |
| 413 | object_property_add_bool(obj, "vnet_hdr_support", |
| 414 | filter_redirector_get_vnet_hdr, |
| 415 | filter_redirector_set_vnet_hdr, NULL); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 416 | } |
| 417 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 418 | static void filter_mirror_fini(Object *obj) |
| 419 | { |
| 420 | MirrorState *s = FILTER_MIRROR(obj); |
| 421 | |
| 422 | g_free(s->outdev); |
| 423 | } |
| 424 | |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 425 | static void filter_redirector_fini(Object *obj) |
| 426 | { |
| 427 | MirrorState *s = FILTER_REDIRECTOR(obj); |
| 428 | |
| 429 | g_free(s->indev); |
| 430 | g_free(s->outdev); |
| 431 | } |
| 432 | |
| 433 | static const TypeInfo filter_redirector_info = { |
| 434 | .name = TYPE_FILTER_REDIRECTOR, |
| 435 | .parent = TYPE_NETFILTER, |
| 436 | .class_init = filter_redirector_class_init, |
| 437 | .instance_init = filter_redirector_init, |
| 438 | .instance_finalize = filter_redirector_fini, |
| 439 | .instance_size = sizeof(MirrorState), |
| 440 | }; |
| 441 | |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 442 | static const TypeInfo filter_mirror_info = { |
| 443 | .name = TYPE_FILTER_MIRROR, |
| 444 | .parent = TYPE_NETFILTER, |
| 445 | .class_init = filter_mirror_class_init, |
| 446 | .instance_init = filter_mirror_init, |
| 447 | .instance_finalize = filter_mirror_fini, |
| 448 | .instance_size = sizeof(MirrorState), |
| 449 | }; |
| 450 | |
| 451 | static void register_types(void) |
| 452 | { |
| 453 | type_register_static(&filter_mirror_info); |
Zhang Chen | d46f75b | 2016-03-17 16:16:26 +0800 | [diff] [blame] | 454 | type_register_static(&filter_redirector_info); |
Zhang Chen | f6d3afb | 2016-03-15 15:41:33 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | type_init(register_types); |