Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 3 | * Copyright (c) 2014-2015 The Linux Foundation. All rights reserved. |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | |
| 17 | struct ecm_classifier_instance; |
| 18 | |
| 19 | /* |
| 20 | * Classifier types. |
| 21 | * MUST BE RECORDED IN ASCENDING ORDER OF PRIORITY |
| 22 | */ |
| 23 | enum ecm_classifier_types { |
| 24 | ECM_CLASSIFIER_TYPE_DEFAULT = 0, /* MUST BE FIRST, Default classifier */ |
Hai Shalom | 81f4e20 | 2014-06-04 09:30:27 -0700 | [diff] [blame] | 25 | #ifdef ECM_CLASSIFIER_HYFI_ENABLE |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 26 | ECM_CLASSIFIER_TYPE_HYFI, /* HyFi classifier */ |
Hai Shalom | 81f4e20 | 2014-06-04 09:30:27 -0700 | [diff] [blame] | 27 | #endif |
Murat Sezgin | b3731e8 | 2014-11-26 12:20:59 -0800 | [diff] [blame] | 28 | #ifdef ECM_CLASSIFIER_DSCP_ENABLE |
Gareth Williams | 8932a91 | 2014-06-11 18:06:25 -0700 | [diff] [blame] | 29 | ECM_CLASSIFIER_TYPE_DSCP, /* Provides DSCP and DSCP remarking support */ |
Murat Sezgin | b3731e8 | 2014-11-26 12:20:59 -0800 | [diff] [blame] | 30 | #endif |
| 31 | #ifdef ECM_CLASSIFIER_NL_ENABLE |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 32 | ECM_CLASSIFIER_TYPE_NL, /* Provides netlink interface */ |
Murat Sezgin | b3731e8 | 2014-11-26 12:20:59 -0800 | [diff] [blame] | 33 | #endif |
Gareth Williams | dcda9b9 | 2015-05-13 10:08:15 +0100 | [diff] [blame] | 34 | #ifdef ECM_CLASSIFIER_PCC_ENABLE |
| 35 | ECM_CLASSIFIER_TYPE_PCC, /* Parental control subsystem support classifier */ |
| 36 | #endif |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 37 | ECM_CLASSIFIER_TYPES, /* MUST BE LAST */ |
| 38 | }; |
| 39 | typedef enum ecm_classifier_types ecm_classifier_type_t; |
| 40 | |
| 41 | /* |
| 42 | * enum ecm_classifier_relevances |
| 43 | * Whether a classifier is relevant to a connection |
| 44 | */ |
| 45 | enum ecm_classifier_relevances { |
| 46 | ECM_CLASSIFIER_RELEVANCE_MAYBE = 0, /* Classifier has not yet determined relevance */ |
| 47 | ECM_CLASSIFIER_RELEVANCE_NO, /* Classifier is not relevant to a connection (classifier will be unassigned from the connection after returning this from a process() call) */ |
| 48 | ECM_CLASSIFIER_RELEVANCE_YES, /* Classifier is relevant to the connection, process actions will be inspected by the front end when returning this from a process() call */ |
| 49 | }; |
| 50 | typedef enum ecm_classifier_relevances ecm_classifier_relevence_t; |
| 51 | |
| 52 | /* |
| 53 | * enum ecm_classifier_acceleration_modes |
| 54 | * Modes in which a connection may be accelerated |
Murat Sezgin | d701ac5 | 2014-08-20 14:02:30 -0700 | [diff] [blame] | 55 | * |
| 56 | * These are used by a classifier to indicate its desire to accelerate. |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 57 | */ |
| 58 | enum ecm_classifier_acceleration_modes { |
| 59 | ECM_CLASSIFIER_ACCELERATION_MODE_DONT_CARE = 0, /* Classifier does not care if the connection is accelerated */ |
| 60 | ECM_CLASSIFIER_ACCELERATION_MODE_NO, /* Connection must not be accelerated */ |
| 61 | ECM_CLASSIFIER_ACCELERATION_MODE_ACCEL, /* Connection can be accelerated whenever */ |
| 62 | }; |
| 63 | typedef enum ecm_classifier_acceleration_modes ecm_classifier_acceleration_mode_t; |
| 64 | |
| 65 | /* |
| 66 | * Process actions |
| 67 | * A process result, that is relevant, may contain zero or more actions for the front end. |
| 68 | * Due to the parallel processing nature of classifiers, *usually* the action(s) of the highest priority |
| 69 | * classifier will override any lower priority actions. This is up to front end discretion, of course. |
| 70 | */ |
Gareth Williams | 27c3db1 | 2014-06-11 17:37:56 -0700 | [diff] [blame] | 71 | #define ECM_CLASSIFIER_PROCESS_ACTION_DROP 0x00000001 /* Drop */ |
| 72 | #define ECM_CLASSIFIER_PROCESS_ACTION_QOS_TAG 0x00000002 /* Contains flow & return qos tags */ |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 73 | #define ECM_CLASSIFIER_PROCESS_ACTION_ACCEL_MODE 0x00000004 /* Contains an accel mode */ |
| 74 | #define ECM_CLASSIFIER_PROCESS_ACTION_TIMER_GROUP 0x00000008 /* Contains a timer group change */ |
Murat Sezgin | 62c9283 | 2015-03-06 16:08:31 -0800 | [diff] [blame] | 75 | |
| 76 | #ifdef ECM_CLASSIFIER_DSCP_ENABLE |
Gareth Williams | bd03e23 | 2014-06-11 17:55:28 -0700 | [diff] [blame] | 77 | #define ECM_CLASSIFIER_PROCESS_ACTION_DSCP 0x00000010 /* Contains DSCP marking information */ |
| 78 | #define ECM_CLASSIFIER_PROCESS_ACTION_DSCP_DENY 0x00000020 /* Denies any DSCP changes */ |
Murat Sezgin | 62c9283 | 2015-03-06 16:08:31 -0800 | [diff] [blame] | 79 | #endif |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * struct ecm_classifier_process_response |
| 83 | * Response structure returned by a process call |
| 84 | */ |
| 85 | struct ecm_classifier_process_response { |
| 86 | ecm_classifier_relevence_t relevance; /* Is this classifier relevant to the connection? */ |
| 87 | uint32_t became_relevant; /* DB time the classifier became relevant or not relevant, if relevance is maybe this field is not relevant! */ |
| 88 | |
| 89 | uint32_t process_actions; /* Actions this process response contains */ |
| 90 | |
| 91 | /* |
| 92 | * The following fields are only to be inspected if this response is relevant AND the process_actions indicates so |
| 93 | */ |
| 94 | bool drop; /* Drop packet at hand */ |
Gareth Williams | 27c3db1 | 2014-06-11 17:37:56 -0700 | [diff] [blame] | 95 | uint32_t flow_qos_tag; /* QoS tag to use for the packet */ |
| 96 | uint32_t return_qos_tag; /* QoS tag to use for the packet */ |
Murat Sezgin | 62c9283 | 2015-03-06 16:08:31 -0800 | [diff] [blame] | 97 | #ifdef ECM_CLASSIFIER_DSCP_ENABLE |
Gareth Williams | bd03e23 | 2014-06-11 17:55:28 -0700 | [diff] [blame] | 98 | uint8_t flow_dscp; /* DSCP mark for flow */ |
| 99 | uint8_t return_dscp; /* DSCP mark for return */ |
Murat Sezgin | 62c9283 | 2015-03-06 16:08:31 -0800 | [diff] [blame] | 100 | #endif |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 101 | ecm_classifier_acceleration_mode_t accel_mode; /* Acceleration needed for this connection */ |
| 102 | ecm_db_timer_group_t timer_group; /* Timer group the connection should be in */ |
| 103 | }; |
| 104 | |
| 105 | /* |
Murat Sezgin | b267606 | 2015-06-12 17:05:31 -0700 | [diff] [blame] | 106 | * Sync rule structure. |
| 107 | * Acceleration engine's sync parameters will be stored |
| 108 | * in this data structure to update the classifiers. |
| 109 | */ |
| 110 | struct ecm_classifier_rule_sync { |
| 111 | uint32_t flow_tx_packet_count; |
| 112 | uint32_t return_tx_packet_count; |
| 113 | uint32_t reason; |
| 114 | }; |
| 115 | |
| 116 | /* |
| 117 | * Create rule structure. |
| 118 | * Additional create rule parameters from the classifiers |
| 119 | * will be copied to this data structure before pushing them to |
| 120 | * the underlying accelaration engine. |
| 121 | */ |
| 122 | struct ecm_classifier_rule_create { |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | /* |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 127 | * To be implemented by all classifiers |
| 128 | */ |
| 129 | typedef void (*ecm_classifier_ref_method_t)(struct ecm_classifier_instance *ci); |
| 130 | typedef int (*ecm_classifier_deref_callback_t)(struct ecm_classifier_instance *ci); |
| 131 | typedef void (*ecm_classifier_process_callback_t)(struct ecm_classifier_instance *ci, ecm_tracker_sender_type_t sender, struct ecm_tracker_ip_header *ip_hdr, struct sk_buff *skb, struct ecm_classifier_process_response *process_response); |
| 132 | /* Process new data for connection, process_response is populated with the response of processing */ |
Murat Sezgin | b267606 | 2015-06-12 17:05:31 -0700 | [diff] [blame] | 133 | typedef void (*ecm_classifier_sync_from_v4_callback_t)(struct ecm_classifier_instance *ci, struct ecm_classifier_rule_create *ecrc); |
| 134 | /* Sync the accel engine state with state from the classifier */ |
| 135 | typedef void (*ecm_classifier_sync_to_v4_callback_t)(struct ecm_classifier_instance *ci, struct ecm_classifier_rule_sync *sync); |
| 136 | /* Sync the classifier state with current accel engine state */ |
| 137 | typedef void (*ecm_classifier_sync_from_v6_callback_t)(struct ecm_classifier_instance *ci, struct ecm_classifier_rule_create *ecrc); |
| 138 | /* Sync the accel engine state with state from the classifier */ |
| 139 | typedef void (*ecm_classifier_sync_to_v6_callback_t)(struct ecm_classifier_instance *ci, struct ecm_classifier_rule_sync *sync); |
| 140 | /* Sync the classifier state with current accel engine state */ |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 141 | typedef ecm_classifier_type_t (*ecm_classifier_type_get_callback_t)(struct ecm_classifier_instance *ci); |
| 142 | /* Get type of classifier this is */ |
| 143 | typedef bool (*ecm_classifier_reclassify_allowed_get_callback_t)(struct ecm_classifier_instance *ci); |
| 144 | /* Get whether reclassification is allowed */ |
| 145 | typedef void (*ecm_classifier_reclassify_callback_t)(struct ecm_classifier_instance *ci); |
| 146 | /* Reclassify */ |
| 147 | typedef void (*ecm_classifier_last_process_response_get_callback_t)(struct ecm_classifier_instance *ci, struct ecm_classifier_process_response *process_response); |
| 148 | /* Get last process response */ |
Gareth Williams | f98d419 | 2015-03-11 16:55:41 +0000 | [diff] [blame] | 149 | #ifdef ECM_STATE_OUTPUT_ENABLE |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 150 | typedef int (*ecm_classifier_state_get_callback_t)(struct ecm_classifier_instance *ci, struct ecm_state_file_instance *sfi); |
| 151 | /* Get state output. Returns 0 upon success. */ |
Gareth Williams | f98d419 | 2015-03-11 16:55:41 +0000 | [diff] [blame] | 152 | #endif |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 153 | |
| 154 | /* |
Murat Sezgin | e34b017 | 2015-11-05 21:58:14 -0800 | [diff] [blame] | 155 | * Determines if a connection should be kept. |
| 156 | */ |
| 157 | typedef bool (*ecm_classifier_should_keep_connection_t) |
| 158 | (struct ecm_classifier_instance *ci, uint8_t *mac); |
| 159 | |
| 160 | /* |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 161 | * Base class for all types of classifiers |
| 162 | */ |
| 163 | struct ecm_classifier_instance { |
| 164 | struct ecm_classifier_instance *ca_next; /* DB use only: Connection assignment next pointer */ |
| 165 | struct ecm_classifier_instance *ca_prev; /* DB use only: Connection assignment prev pointer */ |
| 166 | |
| 167 | ecm_classifier_process_callback_t process; /* Process new skb */ |
| 168 | ecm_classifier_sync_from_v4_callback_t sync_from_v4; |
Murat Sezgin | b267606 | 2015-06-12 17:05:31 -0700 | [diff] [blame] | 169 | /* Sync the accel engine with state from the classifier */ |
| 170 | ecm_classifier_sync_to_v4_callback_t sync_to_v4;/* Sync the classifier with state from the accel engine */ |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 171 | ecm_classifier_sync_from_v6_callback_t sync_from_v6; |
Murat Sezgin | b267606 | 2015-06-12 17:05:31 -0700 | [diff] [blame] | 172 | /* Sync the accel engine with state from the classifier */ |
| 173 | ecm_classifier_sync_to_v6_callback_t sync_to_v6;/* Sync the classifier with state from the accel engine */ |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 174 | ecm_classifier_type_get_callback_t type_get; /* Get type of classifier */ |
| 175 | ecm_classifier_reclassify_allowed_get_callback_t reclassify_allowed; |
| 176 | /* Get whether reclassification is allowed */ |
| 177 | ecm_classifier_reclassify_callback_t reclassify; |
| 178 | /* Reclassify */ |
| 179 | ecm_classifier_last_process_response_get_callback_t last_process_response_get; |
| 180 | /* Return last process response */ |
Murat Sezgin | e34b017 | 2015-11-05 21:58:14 -0800 | [diff] [blame] | 181 | ecm_classifier_should_keep_connection_t should_keep_connection; |
| 182 | /* Check if connection should be kept when FDB updates */ |
Gareth Williams | f98d419 | 2015-03-11 16:55:41 +0000 | [diff] [blame] | 183 | #ifdef ECM_STATE_OUTPUT_ENABLE |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 184 | ecm_classifier_state_get_callback_t state_get; |
| 185 | /* Return its state */ |
Gareth Williams | f98d419 | 2015-03-11 16:55:41 +0000 | [diff] [blame] | 186 | #endif |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 187 | ecm_classifier_ref_method_t ref; |
| 188 | ecm_classifier_deref_callback_t deref; |
| 189 | }; |
| 190 | |
Gareth Williams | f98d419 | 2015-03-11 16:55:41 +0000 | [diff] [blame] | 191 | #ifdef ECM_STATE_OUTPUT_ENABLE |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 192 | /* |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 193 | * ecm_classifier_process_response_state_get() |
| 194 | * Output detail for the process response |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 195 | * |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 196 | * Returns 0 on success. |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 197 | */ |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 198 | static inline int ecm_classifier_process_response_state_get(struct ecm_state_file_instance *sfi, struct ecm_classifier_process_response *pr) |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 199 | { |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 200 | int result; |
| 201 | |
| 202 | if ((result = ecm_state_prefix_add(sfi, "pr"))) { |
| 203 | return result; |
| 204 | } |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 205 | |
| 206 | if (pr->relevance == ECM_CLASSIFIER_RELEVANCE_NO) { |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 207 | return ecm_state_write(sfi, "relevant", "%s", "no"); |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (pr->process_actions & ECM_CLASSIFIER_PROCESS_ACTION_DROP) { |
| 211 | if (pr->drop) { |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 212 | if ((result = ecm_state_write(sfi, "drop", "yes"))) { |
| 213 | return result; |
| 214 | } |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 215 | } else { |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 216 | if ((result = ecm_state_write(sfi, "drop", "no"))) { |
| 217 | return result; |
| 218 | } |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 222 | if (pr->relevance == ECM_CLASSIFIER_RELEVANCE_MAYBE) { |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 223 | if ((result = ecm_state_write(sfi, "accel", "denied"))) { |
| 224 | return result; |
| 225 | } |
| 226 | if ((result = ecm_state_write(sfi, "relevant", "maybe"))) { |
| 227 | return result; |
| 228 | } |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 229 | } else { |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 230 | if ((result = ecm_state_write(sfi, "relevant", "yes"))) { |
| 231 | return result; |
| 232 | } |
| 233 | if (pr->process_actions & ECM_CLASSIFIER_PROCESS_ACTION_ACCEL_MODE) { |
| 234 | if (pr->accel_mode == ECM_CLASSIFIER_ACCELERATION_MODE_ACCEL) { |
| 235 | if ((result = ecm_state_write(sfi, "accel", "wanted"))) { |
| 236 | return result; |
| 237 | } |
| 238 | } |
| 239 | else if (pr->accel_mode == ECM_CLASSIFIER_ACCELERATION_MODE_NO) { |
| 240 | if ((result = ecm_state_write(sfi, "accel", "denied"))) { |
| 241 | return result; |
| 242 | } |
| 243 | } |
| 244 | /* Else don't care */ |
| 245 | } |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Gareth Williams | d5618a8 | 2015-05-20 11:13:32 +0100 | [diff] [blame] | 248 | if (pr->process_actions & ECM_CLASSIFIER_PROCESS_ACTION_QOS_TAG) { |
| 249 | if ((result = ecm_state_write(sfi, "flow_qos_tag", "%u", pr->flow_qos_tag))) { |
| 250 | return result; |
| 251 | } |
| 252 | if ((result = ecm_state_write(sfi, "return_qos_tag", "%u", pr->return_qos_tag))) { |
| 253 | return result; |
| 254 | } |
| 255 | } |
| 256 | #ifdef ECM_CLASSIFIER_DSCP_ENABLE |
| 257 | if (pr->process_actions & ECM_CLASSIFIER_PROCESS_ACTION_DSCP) { |
| 258 | if ((result = ecm_state_write(sfi, "flow_dscp", "%u", pr->flow_dscp))) { |
| 259 | return result; |
| 260 | } |
| 261 | if ((result = ecm_state_write(sfi, "return_dscp", "%u", pr->return_dscp))) { |
| 262 | return result; |
| 263 | } |
| 264 | } |
| 265 | #endif |
| 266 | if (pr->process_actions & ECM_CLASSIFIER_PROCESS_ACTION_TIMER_GROUP) { |
| 267 | if ((result = ecm_state_write(sfi, "timer_group", "%d", pr->timer_group))) { |
| 268 | return result; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return ecm_state_prefix_remove(sfi); |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 273 | } |
Gareth Williams | f98d419 | 2015-03-11 16:55:41 +0000 | [diff] [blame] | 274 | #endif |
| 275 | |
Murat Sezgin | c725ad8 | 2016-04-13 17:12:50 -0700 | [diff] [blame^] | 276 | extern struct ecm_classifier_instance *ecm_classifier_assign_classifier(struct ecm_db_connection_instance *ci, ecm_classifier_type_t type); |
| 277 | extern bool ecm_classifier_reclassify(struct ecm_db_connection_instance *ci, int assignment_count, struct ecm_classifier_instance *assignments[]); |
Ben Menchaca | 84f3663 | 2014-02-28 20:57:38 +0000 | [diff] [blame] | 278 | |