2 * Copyright (c) 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <linux/netfilter/nfnetlink.h>
22 #include "netlink-conntrack.h"
23 #include "netlink-notifier.h"
25 #include "poll-loop.h"
29 enum nl_ct_event_type type;
30 struct ct_dpif_entry entry;
34 event_parse(struct ofpbuf *buf, void *change_)
36 struct test_change *change = change_;
38 return nl_ct_parse_entry(buf, &change->entry, &change->type);
42 event_print(const void *change_, void *aux OVS_UNUSED)
44 const struct test_change *change = change_;
47 struct ds ds = DS_EMPTY_INITIALIZER;
49 nl_ct_format_event_entry(&change->entry, change->type, &ds, true,
51 printf("%s\n", ds_cstr(&ds));
57 test_nl_ct_monitor(struct ovs_cmdl_context *ctx OVS_UNUSED)
60 NFNLGRP_CONNTRACK_DESTROY,
61 NFNLGRP_CONNTRACK_NEW,
62 NFNLGRP_CONNTRACK_UPDATE,
65 struct nln *nlns[ARRAY_SIZE(groups)];
66 struct nln_notifier *notifiers[ARRAY_SIZE(groups)];
68 struct test_change change;
72 for (i = 0; i < ARRAY_SIZE(groups); i++) {
73 nlns[i] = nln_create(NETLINK_NETFILTER, groups[i], event_parse,
76 notifiers[i] = nln_notifier_create(nlns[i], event_print, NULL);
80 for (i = 0; i < ARRAY_SIZE(groups); i++) {
87 for (i = 0; i < ARRAY_SIZE(groups); i++) {
88 nln_notifier_destroy(notifiers[i]);
95 test_nl_ct_dump(struct ovs_cmdl_context *ctx)
97 struct nl_ct_dump_state *dump;
98 uint16_t zone, *pzone = NULL;
99 struct ct_dpif_entry entry;
102 if (ctx->argc >= 2) {
103 if (!ovs_scan(ctx->argv[1], "zone=%"SCNu16, &zone)) {
104 ovs_fatal(0, "Error parsing zone= specifier");
108 err = nl_ct_dump_start(&dump, pzone);
110 ovs_fatal(err, "Error creating conntrack netlink dump");
114 err = nl_ct_dump_next(dump, &entry);
116 struct ds ds = DS_EMPTY_INITIALIZER;
118 ct_dpif_format_entry(&entry, &ds, true, true);
119 printf("%s\n", ds_cstr(&ds));
125 ovs_fatal(err, "Error dumping conntrack netlink entry");
127 nl_ct_dump_done(dump);
132 test_nl_ct_flush(struct ovs_cmdl_context *ctx OVS_UNUSED)
136 if (ctx->argc >= 2) {
139 if (ovs_scan(ctx->argv[1], "zone=%"SCNu16, &zone)) {
140 err = nl_ct_flush_zone(zone);
142 ovs_fatal(0, "Error parsing zone= specifier");
148 ovs_fatal(err, "Error flushing conntrack netlink");
152 static const struct ovs_cmdl_command commands[] = {
153 /* Linux netlink connection tracker interface test. */
155 /* Prints all the entries in the connection table and exits. */
156 {"dump", "[zone=zone]", 0, 1, test_nl_ct_dump},
157 /* Listens to all the connection tracking events and prints them to
158 * standard output until killed. */
159 {"monitor", "", 0, 0, test_nl_ct_monitor},
160 /* Flushes all the entries from all the tables.. */
161 {"flush", "[zone=zone]", 0, 1, test_nl_ct_flush},
163 {NULL, NULL, 0, 0, NULL},
167 test_netlink_conntrack(int argc, char *argv[])
169 struct ovs_cmdl_context ctx = {
173 set_program_name(argv[0]);
174 ovs_cmdl_run_command(&ctx, commands);
177 OVSTEST_REGISTER("test-netlink-conntrack", test_netlink_conntrack);