]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: counters: make it easier to extend the amount of tracked counters
authorWilly Tarreau <w@1wt.eu>
Tue, 28 May 2013 15:40:25 +0000 (17:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 28 May 2013 15:43:40 +0000 (17:43 +0200)
By properly affecting the flags and values, it becomes easier to add
more tracked counters, for example for experimentation. It also slightly
reduces the code and the number of tests. No counters were added with
this patch.

include/proto/proto_tcp.h
include/proto/session.h
include/types/proto_tcp.h
include/types/session.h
src/cfgparse.c
src/peers.c
src/proto_tcp.c
src/session.c

index 08500db6e6a135fe9edb0c89218d51cdb782de8c..20ae2df71d7ef0919f660c397cfc3f0385fb85d5 100644 (file)
@@ -58,6 +58,13 @@ static inline struct stktable_key *addr_to_stktable_key(struct sockaddr_storage
        return static_table_key;
 }
 
+/* for a tcp-request action TCP_ACT_TRK_*, return a tracking index starting at
+ * zero for SC1. Unknown actions also return zero.
+ */
+static inline int tcp_trk_idx(int trk_action)
+{
+       return trk_action - TCP_ACT_TRK_SC1;
+}
 
 #endif /* _PROTO_PROTO_TCP_H */
 
index 3b6e1fcbe1a0482fa54ba2aeac35fdc053de0727..5188639e583f1d66ee7865e92fe0f801ccabbcf6 100644 (file)
@@ -80,17 +80,14 @@ static inline void session_stop_backend_counters(struct session *s)
        void *ptr;
        int i;
 
-       if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
+       if (likely(!(s->flags & SN_BE_TRACK_ANY)))
                return;
 
        for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
                if (!s->stkctr[i].entry)
                        continue;
 
-               if ((i == 0) && !(s->flags & SN_BE_TRACK_SC1))
-                       continue;
-
-               if ((i == 1) && !(s->flags & SN_BE_TRACK_SC2))
+               if (!(s->flags & (SN_BE_TRACK_SC1 << i)))
                        continue;
 
                ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_CONN_CUR);
@@ -100,7 +97,7 @@ static inline void session_stop_backend_counters(struct session *s)
                stksess_kill_if_expired(s->stkctr[i].table, s->stkctr[i].entry);
                s->stkctr[i].entry = NULL;
        }
-       s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
+       s->flags &= ~SN_BE_TRACK_ANY;
 }
 
 /* Increase total and concurrent connection count for stick entry <ts> of table
@@ -169,17 +166,14 @@ static void inline session_inc_be_http_req_ctr(struct session *s)
        void *ptr;
        int i;
 
-       if (likely(!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2))))
+       if (likely(!(s->flags & SN_BE_TRACK_ANY)))
                return;
 
        for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
                if (!s->stkctr[i].entry)
                        continue;
 
-               if ((i == 0) && !(s->flags & SN_BE_TRACK_SC1))
-                       continue;
-
-               if ((i == 1) && !(s->flags & SN_BE_TRACK_SC2))
+               if (!(s->flags & (SN_BE_TRACK_SC1 << i)))
                        continue;
 
                ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_CNT);
index 0ee2ec76c38dab8463f1b54376dbbf72ac37d356..74863b475deffe7007b35e91a6c984df69a373aa 100644 (file)
@@ -32,7 +32,7 @@
 enum {
        TCP_ACT_ACCEPT = 1,
        TCP_ACT_REJECT = 2,
-       TCP_ACT_TRK_SC1 = 3,
+       TCP_ACT_TRK_SC1 = 3, /* TCP request tracking : must be contiguous */
        TCP_ACT_TRK_SC2 = 4,
 };
 
index 7b521824345c8f9880b0585f81a1b4676f4ba472..b5c7460c693579ab99722a970efcbea884a63d5c 100644 (file)
 #define        SN_FINST_SHIFT  16              /* bit shift */
 
 #define SN_IGNORE_PRST 0x00080000      /* ignore persistence */
-#define SN_BE_TRACK_SC1 0x00100000     /* backend tracks stick-counter 1 */
-#define SN_BE_TRACK_SC2 0x00200000     /* backend tracks stick-counter 2 */
 
-#define SN_COMP_READY   0x00400000     /* the compression is initialized */
+#define SN_COMP_READY   0x00100000     /* the compression is initialized */
+
+/* session tracking flags: these ones must absolutely be contiguous */
+#define SN_BE_TRACK_SC1 0x00200000     /* backend tracks stick-counter 1 */
+#define SN_BE_TRACK_SC2 0x00400000     /* backend tracks stick-counter 2 */
+#define SN_BE_TRACK_ANY 0x00600000      /* union of all SN_BE_TRACK_* above */
 
 
 /* WARNING: if new fields are added, they must be initialized in event_accept()
index 9907bfd04aa8f80019911edfd21f6aa2119b2717..18fcedc3de369e16e9bc25a25478d3eca3bbdf58 100644 (file)
@@ -6335,7 +6335,7 @@ int check_config_validity()
                list_for_each_entry(trule, &curproxy->tcp_req.l4_rules, list) {
                        struct proxy *target;
 
-                       if (trule->action != TCP_ACT_TRK_SC1 && trule->action != TCP_ACT_TRK_SC2)
+                       if (trule->action < TCP_ACT_TRK_SC1 || trule->action > TCP_ACT_TRK_SC2)
                                continue;
 
                        if (trule->act_prm.trk_ctr.table.n)
@@ -6346,7 +6346,7 @@ int check_config_validity()
                        if (!target) {
                                Alert("Proxy '%s': unable to find table '%s' referenced by track-sc%d.\n",
                                      curproxy->id, trule->act_prm.trk_ctr.table.n,
-                                     trule->action == TCP_ACT_TRK_SC1 ? 1 : 2);
+                                     1 + tcp_trk_idx(trule->action));
                                cfgerr++;
                        }
                        else if (target->table.size == 0) {
@@ -6357,7 +6357,7 @@ int check_config_validity()
                        else if (!stktable_compatible_sample(trule->act_prm.trk_ctr.expr,  target->table.type)) {
                                Alert("Proxy '%s': stick-table '%s' uses a type incompatible with the 'track-sc%d' rule.\n",
                                      curproxy->id, trule->act_prm.trk_ctr.table.n ? trule->act_prm.trk_ctr.table.n : curproxy->id,
-                                     trule->action == TCP_ACT_TRK_SC1 ? 1 : 2);
+                                     1 + tcp_trk_idx(trule->action));
                                cfgerr++;
                        }
                        else {
@@ -6374,7 +6374,7 @@ int check_config_validity()
                list_for_each_entry(trule, &curproxy->tcp_req.inspect_rules, list) {
                        struct proxy *target;
 
-                       if (trule->action != TCP_ACT_TRK_SC1 && trule->action != TCP_ACT_TRK_SC2)
+                       if (trule->action < TCP_ACT_TRK_SC1 || trule->action > TCP_ACT_TRK_SC2)
                                continue;
 
                        if (trule->act_prm.trk_ctr.table.n)
@@ -6385,7 +6385,7 @@ int check_config_validity()
                        if (!target) {
                                Alert("Proxy '%s': unable to find table '%s' referenced by track-sc%d.\n",
                                      curproxy->id, trule->act_prm.trk_ctr.table.n,
-                                     trule->action == TCP_ACT_TRK_SC1 ? 1 : 2);
+                                     1 + tcp_trk_idx(trule->action));
                                cfgerr++;
                        }
                        else if (target->table.size == 0) {
@@ -6396,7 +6396,7 @@ int check_config_validity()
                        else if (!stktable_compatible_sample(trule->act_prm.trk_ctr.expr,  target->table.type)) {
                                Alert("Proxy '%s': stick-table '%s' uses a type incompatible with the 'track-sc%d' rule.\n",
                                      curproxy->id, trule->act_prm.trk_ctr.table.n ? trule->act_prm.trk_ctr.table.n : curproxy->id,
-                                     trule->action == TCP_ACT_TRK_SC1 ? 1 : 2);
+                                     1 + tcp_trk_idx(trule->action));
                                cfgerr++;
                        }
                        else {
index 5e4ff35a329847ba78c56ecc0179e756f353ed7f..61fb938a3f050b228f85039c2a05e32c137a305d 100644 (file)
@@ -1178,8 +1178,7 @@ static struct session *peer_session_create(struct peer *peer, struct peer_sessio
 
        /* init store persistence */
        s->store_count = 0;
-       s->stkctr[0].entry = NULL;
-       s->stkctr[1].entry = NULL;
+       memset(s->stkctr, 0, sizeof(s->stkctr));
 
        /* FIXME: the logs are horribly complicated now, because they are
         * defined in <p>, <p>, and later <be> and <be>.
index 5638257c4aef61db6702872bb557e6310c4c2516..86a67c4479dbd2b5382cdcca67c89b43e5fc76a9 100644 (file)
@@ -901,8 +901,8 @@ int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
                                        s->flags |= SN_FINST_R;
                                return 0;
                        }
-                       else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
-                                (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
+                       else if ((rule->action >= TCP_ACT_TRK_SC1 && rule->action <= TCP_ACT_TRK_SC2) &&
+                                !s->stkctr[tcp_trk_idx(rule->action)].entry) {
                                /* Note: only the first valid tracking parameter of each
                                 * applies.
                                 */
@@ -912,15 +912,9 @@ int tcp_inspect_request(struct session *s, struct channel *req, int an_bit)
                                key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
 
                                if (key && (ts = stktable_get_entry(t, key))) {
-                                       if (rule->action == TCP_ACT_TRK_SC1) {
-                                               session_track_stkctr(&s->stkctr[0], t, ts);
-                                               if (s->fe != s->be)
-                                                       s->flags |= SN_BE_TRACK_SC1;
-                                       } else {
-                                               session_track_stkctr(&s->stkctr[1], t, ts);
-                                               if (s->fe != s->be)
-                                                       s->flags |= SN_BE_TRACK_SC2;
-                                       }
+                                       session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
+                                       if (s->fe != s->be)
+                                               s->flags |= SN_BE_TRACK_SC1 << tcp_trk_idx(rule->action);
                                }
                        }
                        else {
@@ -1061,8 +1055,8 @@ int tcp_exec_req_rules(struct session *s)
                                result = 0;
                                break;
                        }
-                       else if ((rule->action == TCP_ACT_TRK_SC1 && !s->stkctr[0].entry) ||
-                                (rule->action == TCP_ACT_TRK_SC2 && !s->stkctr[1].entry)) {
+                       else if ((rule->action >= TCP_ACT_TRK_SC1 && rule->action <= TCP_ACT_TRK_SC2) &&
+                                !s->stkctr[tcp_trk_idx(rule->action)].entry) {
                                /* Note: only the first valid tracking parameter of each
                                 * applies.
                                 */
@@ -1071,12 +1065,8 @@ int tcp_exec_req_rules(struct session *s)
                                t = rule->act_prm.trk_ctr.table.t;
                                key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr);
 
-                               if (key && (ts = stktable_get_entry(t, key))) {
-                                       if (rule->action == TCP_ACT_TRK_SC1)
-                                               session_track_stkctr(&s->stkctr[0], t, ts);
-                                       else
-                                               session_track_stkctr(&s->stkctr[1], t, ts);
-                               }
+                               if (key && (ts = stktable_get_entry(t, key)))
+                                       session_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
                        }
                        else {
                                /* otherwise it's an accept */
@@ -1193,11 +1183,7 @@ static int tcp_parse_request_rule(char **args, int arg, int section_type,
                        arg++;
                }
                rule->act_prm.trk_ctr.expr = expr;
-
-               if (args[kw][8] == '1')
-                       rule->action = TCP_ACT_TRK_SC1;
-               else
-                       rule->action = TCP_ACT_TRK_SC2;
+               rule->action = TCP_ACT_TRK_SC1 + args[kw][8] - '1';
        }
        else {
                memprintf(err,
index d44583b1e6a7efbbd92dad57d0ecfc5ec053fdf7..404fd0605107a7fb220fa89ea802048df3191cdf 100644 (file)
@@ -97,10 +97,8 @@ int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
         */
        s->flags = 0;
        s->logs.logwait = p->to_log;
-       s->stkctr[0].entry = NULL;
-       s->stkctr[1].entry = NULL;
-       s->stkctr[0].table = NULL;
-       s->stkctr[1].table = NULL;
+
+       memset(s->stkctr, 0, sizeof(s->stkctr));
 
        s->listener = l;
        s->fe  = p;