aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/utils
diff options
context:
space:
mode:
authorJordan Augé <jordan.auge+fdio@cisco.com>2019-07-26 23:20:30 +0200
committerMauro Sardara <msardara@cisco.com>2019-07-29 17:13:35 +0200
commit0a1c6b5565e20167d1f1f33a5a8b597f420b18b0 (patch)
tree98c5da8f231fbd3dc2ce6502040e29c8333d9ffc /hicn-light/src/hicn/utils
parent05ca0aa8f612ee48fb66d4dbebe596b7f1e03181 (diff)
[HICN-252] Add per-application policy framework to hicn-light forwarder
Change-Id: I0531cd7a7de179581295ae34766c81cd9cf3e172 Signed-off-by: Jordan Augé <jordan.auge+fdio@cisco.com> Signed-off-by: Mauro Sardara <msardara@cisco.com> Co-authored-by: Mauro Sardara <msardara@cisco.com>
Diffstat (limited to 'hicn-light/src/hicn/utils')
-rw-r--r--hicn-light/src/hicn/utils/CMakeLists.txt3
-rw-r--r--hicn-light/src/hicn/utils/commands.h51
-rw-r--r--hicn-light/src/hicn/utils/policy.c59
-rw-r--r--hicn-light/src/hicn/utils/policy.h232
-rw-r--r--hicn-light/src/hicn/utils/token.h40
5 files changed, 385 insertions, 0 deletions
diff --git a/hicn-light/src/hicn/utils/CMakeLists.txt b/hicn-light/src/hicn/utils/CMakeLists.txt
index ad9c8e4c1..dd9e12c15 100644
--- a/hicn-light/src/hicn/utils/CMakeLists.txt
+++ b/hicn-light/src/hicn/utils/CMakeLists.txt
@@ -19,7 +19,9 @@ list(APPEND HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/commands.h
${CMAKE_CURRENT_SOURCE_DIR}/interface.h
${CMAKE_CURRENT_SOURCE_DIR}/interfaceSet.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/policy.h
${CMAKE_CURRENT_SOURCE_DIR}/punting.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/token.h
${CMAKE_CURRENT_SOURCE_DIR}/utils.h
)
@@ -28,6 +30,7 @@ list(APPEND SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/addressList.c
${CMAKE_CURRENT_SOURCE_DIR}/interface.c
${CMAKE_CURRENT_SOURCE_DIR}/interfaceSet.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/policy.c
${CMAKE_CURRENT_SOURCE_DIR}/punting.c
${CMAKE_CURRENT_SOURCE_DIR}/utils.c
)
diff --git a/hicn-light/src/hicn/utils/commands.h b/hicn-light/src/hicn/utils/commands.h
index 96415d8da..223bbffaa 100644
--- a/hicn-light/src/hicn/utils/commands.h
+++ b/hicn-light/src/hicn/utils/commands.h
@@ -31,6 +31,8 @@
#include <stdint.h>
#include <stdlib.h>
+#include <hicn/utils/policy.h>
+
typedef struct in6_addr ipv6_addr_t;
typedef uint32_t ipv4_addr_t;
@@ -67,6 +69,12 @@ typedef enum {
MAPME_TIMESCALE,
MAPME_RETX,
CONNECTION_SET_ADMIN_STATE,
+#ifdef WITH_POLICY
+ ADD_POLICY,
+ LIST_POLICIES,
+ REMOVE_POLICY,
+ UPDATE_CONNECTION,
+#endif /* WITH_POLICY */
LAST_COMMAND_VALUE
} command_id;
@@ -129,6 +137,9 @@ typedef struct {
uint8_t ipType;
uint8_t connectionType;
uint8_t admin_state;
+#ifdef WITH_POLICY
+ policy_tags_t tags;
+#endif /* WITH_POLICY */
} add_connection_command;
// SIZE=56
@@ -291,6 +302,36 @@ typedef struct {
uint8_t admin_state;
} connection_set_admin_state_command;
+#ifdef WITH_POLICY
+
+typedef struct {
+ union commandAddr address;
+ uint8_t addressType;
+ uint8_t len;
+ policy_t policy;
+} add_policy_command;
+
+typedef struct {
+ union commandAddr address;
+ uint8_t addressType;
+ uint8_t len;
+ policy_t policy;
+} list_policies_command;
+
+typedef struct {
+ union commandAddr address;
+ uint8_t addressType;
+ uint8_t len;
+} remove_policy_command;
+
+typedef struct {
+ char symbolicOrConnid[16];
+ uint8_t admin_state;
+ policy_tags_t tags;
+} update_connection_command;
+
+#endif /* WITH_POLICY */
+
//===== size of commands ======
// REMINDER: when a new_command is added, the following switch has to be
// updated.
@@ -334,6 +375,16 @@ static inline int payloadLengthDaemon(command_id id) {
return sizeof(mapme_timing_command);
case CONNECTION_SET_ADMIN_STATE:
return sizeof(connection_set_admin_state_command);
+#ifdef WITH_POLICY
+ case ADD_POLICY:
+ return sizeof(add_policy_command);
+ case LIST_POLICIES:
+ return 0; // list policies: payload always 0
+ case REMOVE_POLICY:
+ return sizeof(remove_policy_command);
+ case UPDATE_CONNECTION:
+ return sizeof(update_connection_command);
+#endif /* WITH_POLICY */
case LAST_COMMAND_VALUE:
return 0;
default:
diff --git a/hicn-light/src/hicn/utils/policy.c b/hicn-light/src/hicn/utils/policy.c
new file mode 100644
index 000000000..6c8651ee3
--- /dev/null
+++ b/hicn-light/src/hicn/utils/policy.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * \file policy.h
+ * \brief Implementation of policy description
+ */
+
+#include <stdio.h>
+#include "policy.h"
+
+const char * policy_tag_str[] = {
+ #define _(x, y) [POLICY_TAG_ ## x] = STRINGIZE(x),
+ foreach_policy_tag
+ #undef _
+};
+
+const char policy_tag_short_str[] = {
+ #define _(x, y) [POLICY_TAG_ ## x] = y,
+ foreach_policy_tag
+ #undef _
+};
+
+const char * policy_state_str[] = {
+ #define _(x) [POLICY_STATE_ ## x] = STRINGIZE(x),
+ foreach_policy_state
+ #undef _
+};
+
+int
+policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state)
+{
+ char *cur = s;
+ int rc;
+
+ if (tag_state->disabled > 1)
+ return -1;
+
+ rc = snprintf(cur, s + size - cur, "%s%s", (tag_state->disabled == 1) ? "!" : "", policy_state_str[tag_state->state]);
+ if (rc < 0)
+ return rc;
+ cur += rc;
+ if (size != 0 && cur >= s + size)
+ return cur - s;
+
+ return cur - s;
+}
diff --git a/hicn-light/src/hicn/utils/policy.h b/hicn-light/src/hicn/utils/policy.h
new file mode 100644
index 000000000..993fdca44
--- /dev/null
+++ b/hicn-light/src/hicn/utils/policy.h
@@ -0,0 +1,232 @@
+/*
+ * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * \file policy.h
+ * \brief Policy description
+ */
+#ifndef HICN_POLICY_H
+#define HICN_POLICY_H
+
+#include <netinet/in.h> // INET*_ADDRSTRLEN
+#include <string.h> // strcasecmp
+#include "token.h"
+
+/* POLICY TAG */
+
+#define foreach_policy_tag \
+ /* Interface type */ \
+ _(WIRED, 'E') \
+ _(WIFI, 'W') \
+ _(CELLULAR, 'C') \
+ /* QoS */ \
+ _(BEST_EFFORT, 'b') \
+ _(REALTIME, 'r') \
+ _(MULTIPATH, 'M') \
+ /* Security */ \
+ _(TRUSTED, 'T')
+
+typedef enum {
+#define _(x, y) POLICY_TAG_ ## x,
+foreach_policy_tag
+#undef _
+ POLICY_TAG_N
+} policy_tag_t;
+
+#define MAXSZ_POLICY_TAG_ 11
+#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1
+
+extern const char * policy_tag_str[];
+extern const char policy_tag_short_str[];
+
+static inline
+policy_tag_t
+policy_tag_from_str(const char * str)
+{
+#define _(x, y) if (strcasecmp(str, policy_tag_str[POLICY_TAG_ ## x] ) == 0) { return POLICY_TAG_ ## x; } else
+foreach_policy_tag
+#undef _
+ return POLICY_TAG_N;
+}
+
+/* POLICY_TAGS */
+
+typedef int policy_tags_t;
+
+static inline
+void policy_tags_add(policy_tags_t * tags, policy_tag_t tag)
+{
+ *tags |= (1 << tag);
+}
+
+static inline
+void policy_tags_remove(policy_tags_t * tags, policy_tag_t tag)
+{
+ *tags &= ~(1 << tag);
+}
+
+static inline
+int policy_tags_has(policy_tags_t tags, policy_tag_t tag)
+{
+ return tags & (1 << tag);
+}
+
+#define POLICY_TAGS_EMPTY 0
+
+static inline
+int
+policy_tags_snprintf(char * s, size_t size, policy_tags_t tags)
+{
+#define _(x, y) s[POLICY_TAG_ ## x] = policy_tags_has(tags, POLICY_TAG_ ## x) ? y : '.';
+foreach_policy_tag
+#undef _
+ s[POLICY_TAG_N] = '\0';
+ return POLICY_TAG_N + 1;
+}
+
+#define MAXSZ_POLICY_TAGS_ POLICY_TAG_N
+#define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1
+
+/* POLICY STATE */
+
+#define foreach_policy_state \
+ _(NEUTRAL) \
+ _(REQUIRE) \
+ _(PREFER) \
+ _(AVOID) \
+ _(PROHIBIT) \
+ _(N)
+
+typedef enum {
+#define _(x) POLICY_STATE_ ## x,
+foreach_policy_state
+#undef _
+} policy_state_t;
+
+#define MAXSZ_POLICY_STATE_ 8
+#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1
+
+extern const char * policy_state_str[];
+
+
+/* POLICY TAG STATE */
+
+typedef struct {
+ policy_state_t state;
+ uint8_t disabled;
+} policy_tag_state_t;
+
+#define MAXSZ_POLICY_TAG_STATE_ 8
+#define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1
+
+int policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state);
+
+
+/* INTERFACE STATS */
+
+typedef struct {
+ float throughput;
+ float latency;
+ float loss_rate;
+} interface_stats_t;
+
+#define INTERFACE_STATS_NONE { \
+ .throughput = 0, \
+ .latency = 0, \
+ .loss_rate = 0, \
+}
+
+
+/* POLICY STATS */
+
+typedef struct {
+ interface_stats_t wired;
+ interface_stats_t wifi;
+ interface_stats_t cellular;
+ interface_stats_t all;
+} policy_stats_t;
+
+#define POLICY_STATS_NONE { \
+ .wired = INTERFACE_STATS_NONE, \
+ .wifi = INTERFACE_STATS_NONE, \
+ .cellular = INTERFACE_STATS_NONE, \
+ .all = INTERFACE_STATS_NONE, \
+}
+
+typedef struct {
+ uint32_t num_packets;
+ uint32_t num_bytes;
+ uint32_t num_losses;
+ uint32_t latency_idle;
+} interface_counters_t;
+
+#define INTERFACE_COUNTERS_NONE { \
+ .num_packets = 0, \
+ .num_bytes = 0, \
+ .num_losses = 0, \
+ .latency_idle = 0, \
+}
+
+typedef struct {
+ interface_counters_t wired;
+ interface_counters_t wifi;
+ interface_counters_t cellular;
+ interface_counters_t all;
+ uint64_t last_update;
+} policy_counters_t;
+
+#define POLICY_COUNTERS_NONE (policy_counters_t) { \
+ .wired = INTERFACE_COUNTERS_NONE, \
+ .wifi = INTERFACE_COUNTERS_NONE, \
+ .cellular = INTERFACE_COUNTERS_NONE, \
+ .all = INTERFACE_COUNTERS_NONE, \
+ .last_update = 0, \
+}
+
+/* POLICY */
+
+#define APP_NAME_LEN 128
+
+typedef struct {
+ char app_name[APP_NAME_LEN];
+ policy_tag_state_t tags[POLICY_TAG_N];
+ policy_stats_t stats;
+} policy_t;
+
+static const policy_t POLICY_NONE = {
+ .app_name = { 0 },
+ .tags = {
+#define _(x, y) [POLICY_TAG_ ## x] = { POLICY_STATE_NEUTRAL, 0 },
+foreach_policy_tag
+#undef _
+ },
+ .stats = POLICY_STATS_NONE,
+};
+
+
+/* POLICY DESCRIPTION */
+
+#define PFX_STRLEN 4 /* eg. /128 */
+
+typedef struct {
+ int family;
+ union {
+ char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN];
+ char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN];
+ };
+ policy_t policy;
+} policy_description_t;
+
+#endif /* HICN_POLICY_H */
diff --git a/hicn-light/src/hicn/utils/token.h b/hicn-light/src/hicn/utils/token.h
new file mode 100644
index 000000000..43e0a77b2
--- /dev/null
+++ b/hicn-light/src/hicn/utils/token.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Token concatenation */
+
+/*
+ * Concatenate preprocessor tokens A and B without expanding macro definitions
+ * (however, if invoked from a macro, macro arguments are expanded).
+ */
+#define PPCAT_NX(A, B) A ## B
+
+/*
+ * Concatenate preprocessor tokens A and B after macro-expanding them.
+ */
+#define PPCAT(A, B) PPCAT_NX(A, B)
+
+/* Token stringification */
+
+/*
+ * Turn A into a string literal without expanding macro definitions
+ * (however, if invoked from a macro, macro arguments are expanded).
+ */
+#define STRINGIZE_NX(A) #A
+
+/*
+ * Turn A into a string literal after macro-expanding it.
+ */
+#define STRINGIZE(A) STRINGIZE_NX(A)