]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: applet: Add flags on the appctx and stop abusing its state
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 15 Jan 2024 08:04:08 +0000 (09:04 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 7 Feb 2024 14:03:34 +0000 (15:03 +0100)
Till now, we've extended the appctx state to add some flags. However, the
field name is misleading. So a bitfield was added to handle real flags. And
helper functions to manipulate this bitfield were added.

include/haproxy/applet-t.h
include/haproxy/applet.h
src/applet.c

index 51b3b4c8b025c7eeb952058eb70c0f0b33da702c..71fb57ecd96f22584845d48ab7cd838f756732bf 100644 (file)
 #include <haproxy/xref-t.h>
 
 /* flags for appctx->state */
-#define APPLET_WANT_DIE     0x01  /* applet was running and requested to die */
-#define APPLET_INBLK_ALLOC  0x02
-#define APPLET_INBLK_FULL   0x04
-#define APPLET_OUTBLK_ALLOC 0x08
-#define APPLET_OUTBLK_FULL  0x10
+#define APPLET_WANT_DIE      0x01  /* applet was running and requested to die */
 
 /* Room for per-command context (mostly CLI commands but not only) */
 #define APPLET_MAX_SVCCTX 88
 
+/* Appctx Flags */
+#define APPCTX_FL_INBLK_ALLOC    0x00000001
+#define APPCTX_FL_INBLK_FULL     0x00000002
+#define APPCTX_FL_OUTBLK_ALLOC   0x00000004
+#define APPCTX_FL_OUTBLK_FULL    0x00000008
+
 struct appctx;
 struct proxy;
 struct stconn;
@@ -69,6 +71,7 @@ struct appctx {
        unsigned int st0;          /* CLI state for stats, session state for peers */
        unsigned int st1;          /* prompt/payload (bitwise OR of APPCTX_CLI_ST1_*) for stats, session error for peers */
 
+       unsigned int flags;        /* APPCTX_FL_* */
        struct buffer inbuf;
        struct buffer outbuf;
 
index 9b4f5744b25a897d93d2c3203565ba503fb60ddb..6915eb3fc41a1f88a6f649e1820e411a40007796 100644 (file)
@@ -145,6 +145,36 @@ static inline struct stream *appctx_strm(const struct appctx *appctx)
        return __sc_strm(appctx->sedesc->sc);
 }
 
+static forceinline void applet_fl_zero(struct appctx *appctx)
+{
+       appctx->flags = 0;
+}
+
+static forceinline void applet_fl_setall(struct appctx *appctx, uint all)
+{
+       appctx->flags = all;
+}
+
+static forceinline void applet_fl_set(struct appctx *appctx, uint on)
+{
+       appctx->flags |= on;
+}
+
+static forceinline void applet_fl_clr(struct appctx *appctx, uint off)
+{
+       appctx->flags &= ~off;
+}
+
+static forceinline uint applet_fl_test(const struct appctx *appctx, uint test)
+{
+       return !!(appctx->flags & test);
+}
+
+static forceinline uint applet_fl_get(const struct appctx *appctx)
+{
+       return appctx->flags;
+}
+
 /* The applet announces it has more data to deliver to the stream's input
  * buffer.
  */
index d93e37a40fb2330689d3f98191e4ee74f8e38acd..27c1f7f4042ebb08afa9a101bbb66061c55a05fe 100644 (file)
@@ -264,6 +264,7 @@ struct appctx *appctx_new_on(struct applet *applet, struct sedesc *sedesc, int t
                appctx->t->process = task_run_applet;
        appctx->t->context = appctx;
 
+       appctx->flags = 0;
        appctx->inbuf = BUF_NULL;
        appctx->outbuf = BUF_NULL;
 
@@ -414,15 +415,15 @@ int appctx_buf_available(void *arg)
        struct appctx *appctx = arg;
        struct stconn *sc = appctx_sc(appctx);
 
-       if ((appctx->state & APPLET_INBLK_ALLOC) && b_alloc(&appctx->inbuf)) {
-               appctx->state &= ~APPLET_INBLK_ALLOC;
+       if (applet_fl_test(appctx, APPCTX_FL_INBLK_ALLOC) && b_alloc(&appctx->inbuf)) {
+               applet_fl_clr(appctx, APPCTX_FL_INBLK_ALLOC);
                TRACE_STATE("unblocking appctx, inbuf allocated", APPLET_EV_RECV|APPLET_EV_BLK|APPLET_EV_WAKE, appctx);
                task_wakeup(appctx->t, TASK_WOKEN_RES);
                return 1;
        }
 
-       if ((appctx->state & APPLET_OUTBLK_ALLOC) && b_alloc(&appctx->outbuf)) {
-               appctx->state &= ~APPLET_OUTBLK_ALLOC;
+       if (applet_fl_test(appctx, APPCTX_FL_OUTBLK_ALLOC) && b_alloc(&appctx->outbuf)) {
+               applet_fl_clr(appctx, APPCTX_FL_OUTBLK_ALLOC);
                TRACE_STATE("unblocking appctx, outbuf allocated", APPLET_EV_SEND|APPLET_EV_BLK|APPLET_EV_WAKE, appctx);
                task_wakeup(appctx->t, TASK_WOKEN_RES);
                return 1;
@@ -455,14 +456,14 @@ size_t appctx_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, unsig
 
        TRACE_ENTER(APPLET_EV_RECV, appctx);
 
-       if (appctx->state & APPLET_OUTBLK_ALLOC)
+       if (applet_fl_test(appctx, APPCTX_FL_OUTBLK_ALLOC))
                goto end;
 
        if (!count)
                goto end;
 
        if (!appctx_get_buf(appctx, &appctx->outbuf)) {
-               appctx->state |= APPLET_OUTBLK_ALLOC;
+               applet_fl_set(appctx, APPCTX_FL_OUTBLK_ALLOC);
                TRACE_STATE("waiting for appctx outbuf allocation", APPLET_EV_RECV|APPLET_EV_BLK, appctx);
                goto end;
        }
@@ -505,7 +506,7 @@ size_t appctx_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, unsig
 
   done:
        if (ret)
-               appctx->state |= APPLET_OUTBLK_FULL;
+               applet_fl_clr(appctx, APPCTX_FL_OUTBLK_FULL);
 
        if (b_data(&appctx->outbuf)) {
                se_fl_set(appctx->sedesc, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
@@ -528,14 +529,14 @@ size_t appctx_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, unsig
 
        TRACE_ENTER(APPLET_EV_SEND, appctx);
 
-       if (appctx->state & (APPLET_INBLK_FULL|APPLET_INBLK_ALLOC))
+       if (applet_fl_test(appctx, (APPCTX_FL_INBLK_FULL|APPCTX_FL_INBLK_ALLOC)))
                goto end;
 
        if (!count)
                goto end;
 
        if (!appctx_get_buf(appctx, &appctx->inbuf)) {
-               appctx->state |= APPLET_INBLK_ALLOC;
+               applet_fl_set(appctx, APPCTX_FL_INBLK_ALLOC);
                TRACE_STATE("waiting for appctx inbuf allocation", APPLET_EV_SEND|APPLET_EV_BLK, appctx);
                goto end;
        }
@@ -567,7 +568,7 @@ size_t appctx_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, unsig
 
   done:
        if (ret < count) {
-               appctx->state |= APPLET_INBLK_FULL;
+               applet_fl_set(appctx, APPCTX_FL_INBLK_FULL);
                TRACE_STATE("report appctx inbuf is full", APPLET_EV_SEND|APPLET_EV_BLK, appctx);
        }