Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing applets |
| 3 | * |
| 4 | * Copyright 2000-2015 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <common/config.h> |
| 17 | #include <common/mini-clist.h> |
| 18 | #include <proto/applet.h> |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 19 | #include <proto/channel.h> |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 20 | #include <proto/stream.h> |
| 21 | #include <proto/stream_interface.h> |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 22 | #include <proto/task.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 23 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 24 | unsigned int nb_applets = 0; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 26 | /* Callback used to wake up an applet when a buffer is available. The applet |
| 27 | * <appctx> is woken up if an input buffer was requested for the associated |
| 28 | * stream interface. In this case the buffer is immediately allocated and the |
| 29 | * function returns 1. Otherwise it returns 0. Note that this automatically |
| 30 | * covers multiple wake-up attempts by ensuring that the same buffer will not |
| 31 | * be accounted for multiple times. |
| 32 | */ |
| 33 | int appctx_buf_available(void *arg) |
| 34 | { |
| 35 | struct appctx *appctx = arg; |
| 36 | struct stream_interface *si = appctx->owner; |
| 37 | |
| 38 | /* allocation requested ? */ |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 39 | if (!(si->flags & SI_FL_RXBLK_BUFF)) |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 40 | return 0; |
| 41 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 42 | si_rx_buff_rdy(si); |
| 43 | |
| 44 | /* was already allocated another way ? if so, don't take this one */ |
| 45 | if (c_size(si_ic(si)) || si_ic(si)->pipe) |
| 46 | return 0; |
| 47 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 48 | /* allocation possible now ? */ |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 49 | if (!b_alloc_margin(&si_ic(si)->buf, global.tune.reserved_bufs)) { |
| 50 | si_rx_buff_blk(si); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 51 | return 0; |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 52 | } |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 54 | task_wakeup(appctx->t, TASK_WOKEN_RES); |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | /* Default applet handler */ |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 59 | struct task *task_run_applet(struct task *t, void *context, unsigned short state) |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 60 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 61 | struct appctx *app = context; |
| 62 | struct stream_interface *si = app->owner; |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 63 | unsigned int rate; |
Christopher Faulet | b4a4d9a | 2017-11-15 22:14:49 +0100 | [diff] [blame] | 64 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 65 | if (app->state & APPLET_WANT_DIE) { |
| 66 | __appctx_free(app); |
| 67 | return NULL; |
Christopher Faulet | 7163056 | 2017-11-14 11:30:47 +0100 | [diff] [blame] | 68 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 69 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 70 | /* We always pretend the applet can't get and doesn't want to |
| 71 | * put, it's up to it to change this if needed. This ensures |
| 72 | * that one applet which ignores any event will not spin. |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 73 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 74 | si_cant_get(si); |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 75 | si_rx_endp_done(si); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 22d63a2 | 2019-04-24 08:41:29 +0200 | [diff] [blame] | 77 | /* measure the call rate */ |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 78 | rate = update_freq_ctr(&app->call_rate, 1); |
| 79 | if (rate >= 100000 && app->call_rate.prev_ctr) { // make sure to wait at least a full second |
| 80 | stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate)); |
| 81 | } |
Willy Tarreau | 22d63a2 | 2019-04-24 08:41:29 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 83 | /* Now we'll try to allocate the input buffer. We wake up the applet in |
| 84 | * all cases. So this is the applet's responsibility to check if this |
| 85 | * buffer was allocated or not. This leaves a chance for applets to do |
| 86 | * some other processing if needed. The applet doesn't have anything to |
| 87 | * do if it needs the buffer, it will be called again upon readiness. |
| 88 | */ |
| 89 | if (!si_alloc_ibuf(si, &app->buffer_wait)) |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 90 | si_rx_endp_more(si); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 91 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 92 | app->applet->fct(app); |
| 93 | si_applet_wake_cb(si); |
| 94 | channel_release_buffer(si_ic(si), &app->buffer_wait); |
| 95 | return t; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 96 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 97 | |