[PATCH nft 2/4] src: osf: add json support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Florian Westphal <fw@xxxxxxxxx>
---
 include/json.h           |  2 ++
 src/json.c               |  5 ++++
 src/osf.c                |  2 ++
 src/parser_json.c        | 17 +++++++++++-
 tests/py/inet/osf.t.json | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100644 tests/py/inet/osf.t.json

diff --git a/include/json.h b/include/json.h
index b75512b8475f..78c026a7a7f6 100644
--- a/include/json.h
+++ b/include/json.h
@@ -40,6 +40,7 @@ json_t *hash_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *fib_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *constant_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *osf_expr_json(const struct expr *expr, struct output_ctx *octx);
 
 json_t *integer_type_json(const struct expr *expr, struct output_ctx *octx);
 json_t *string_type_json(const struct expr *expr, struct output_ctx *octx);
@@ -118,6 +119,7 @@ EXPR_PRINT_STUB(hash_expr)
 EXPR_PRINT_STUB(fib_expr)
 EXPR_PRINT_STUB(constant_expr)
 EXPR_PRINT_STUB(socket_expr)
+EXPR_PRINT_STUB(osf_expr)
 
 EXPR_PRINT_STUB(integer_type)
 EXPR_PRINT_STUB(string_type)
diff --git a/src/json.c b/src/json.c
index 8a3e15e659eb..6112cf563125 100644
--- a/src/json.c
+++ b/src/json.c
@@ -811,6 +811,11 @@ json_t *socket_expr_json(const struct expr *expr, struct output_ctx *octx)
 			 socket_templates[expr->socket.key].token);
 }
 
+json_t *osf_expr_json(const struct expr *expr, struct output_ctx *octx)
+{
+	return json_pack("{s:{s:s}}", "osf", "key", "name");
+}
+
 json_t *integer_type_json(const struct expr *expr, struct output_ctx *octx)
 {
 	char buf[1024] = "0x";
diff --git a/src/osf.c b/src/osf.c
index 131d54e4afbf..85c957391b57 100644
--- a/src/osf.c
+++ b/src/osf.c
@@ -3,6 +3,7 @@
 #include <utils.h>
 #include <string.h>
 #include <osf.h>
+#include <json.h>
 
 static void osf_expr_print(const struct expr *expr, struct output_ctx *octx)
 {
@@ -18,6 +19,7 @@ static const struct expr_ops osf_expr_ops = {
 	.name		= "osf",
 	.print		= osf_expr_print,
 	.clone		= osf_expr_clone,
+	.json		= osf_expr_json,
 };
 
 struct expr *osf_expr_alloc(const struct location *loc)
diff --git a/src/parser_json.c b/src/parser_json.c
index b51f95e1c7b7..6d8cda975ff9 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -346,13 +346,27 @@ static struct expr *json_parse_meta_expr(struct json_ctx *ctx,
 	return meta_expr_alloc(int_loc, key);
 }
 
+static struct expr *json_parse_osf_expr(struct json_ctx *ctx,
+					const char *type, json_t *root)
+{
+	const char *key;
+
+	if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
+		return NULL;
+
+	if (!strcmp(key, "name"))
+		return osf_expr_alloc(int_loc);
+
+	json_error(ctx, "Invalid osf key value.");
+	return NULL;
+}
+
 static struct expr *json_parse_socket_expr(struct json_ctx *ctx,
 					   const char *type, json_t *root)
 {
 	const char *key;
 	int keyval = -1;
 
-
 	if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
 		return NULL;
 
@@ -1181,6 +1195,7 @@ static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root)
 		{ "exthdr", json_parse_exthdr_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP },
 		{ "tcp option", json_parse_tcp_option_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES },
 		{ "meta", json_parse_meta_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP },
+		{ "osf", json_parse_osf_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_MAP },
 		{ "socket", json_parse_socket_expr, CTX_F_PRIMARY },
 		{ "rt", json_parse_rt_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP },
 		{ "ct", json_parse_ct_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP },
diff --git a/tests/py/inet/osf.t.json b/tests/py/inet/osf.t.json
new file mode 100644
index 000000000000..59b32a3bab5c
--- /dev/null
+++ b/tests/py/inet/osf.t.json
@@ -0,0 +1,67 @@
+# osf name "Linux"
+[
+    {
+        "match": {
+            "left": {
+                "osf": {
+                    "key": "name"
+                }
+            },
+            "right": "Linux"
+        }
+    }
+]
+
+# osf name { "Windows", "MacOs" }
+[
+    {
+        "match": {
+            "left": {
+                "osf": {
+                    "key": "name"
+                }
+            },
+            "right": {
+                "set": [
+                    "MacOs",
+                    "Windows"
+                ]
+            }
+        }
+    }
+]
+
+# ct mark set osf name map { "Windows" : 0x00000001, "MacOs" : 0x00000002 }
+[
+    {
+        "mangle": {
+            "left": {
+                "ct": {
+                    "key": "mark"
+                }
+            },
+            "right": {
+                "map": {
+                    "left": {
+                        "osf": {
+                            "key": "name"
+                        }
+                    },
+                    "right": {
+                        "set": [
+                            [
+                                "MacOs",
+                                2
+                            ],
+                            [
+                                "Windows",
+                                1
+                            ]
+                        ]
+                    }
+                }
+            }
+        }
+    }
+]
+
-- 
2.16.4




[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux