1 //--------------------------------------------------------------------------
2 // Copyright (C) 2016-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation.  You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //--------------------------------------------------------------------------
18 
19 // context_switcher.h author Russ Combs <rucombs@cisco.com>
20 
21 #ifndef CONTEXT_SWITCHER_H
22 #define CONTEXT_SWITCHER_H
23 
24 // ContextSwitcher maintains a set of contexts, only one of which can be
25 // active at any time. the normal workflow is:
26 //
27 // 1.  start and stop are called at the beginning and end of each wire
28 // packet which activates and releases one context from among those
29 // available.
30 //
31 // 2.  during processing interrupt and complete should be called to start
32 // and finish processing of a generated pseudo packet. it is possible to
33 // interrupt pseudo packets. complete may return without doing anything if
34 // dependent contexts were suspended.
35 //
36 // 3.  suspend may be called to pause the current context and activate the
37 // prior. multiple contexts may be suspended.
38 //
39 // 4.  there is no ordering of idle contexts. busy contexts are in strict LIFO
40 // order. context dependency chains are maintained in depth-first order by Flow.
41 
42 #include <vector>
43 
44 #include "detection/ips_context_chain.h"
45 #include "utils/primed_allocator.h"
46 
47 namespace snort
48 {
49 class Flow;
50 class IpsContext;
51 class IpsContextData;
52 }
53 
54 // FIXIT-E add the hold to catch offloads that don't return
55 class ContextSwitcher
56 {
57 public:
58     ~ContextSwitcher();
59 
60     void push(snort::IpsContext*);
61 
62     void start();
63     void stop();
64     void abort();
65 
66     snort::IpsContext* interrupt();
67     snort::IpsContext* complete();
68 
69     void suspend();
70     void resume(snort::IpsContext*);
71 
72     snort::IpsContext* get_context() const;
73     snort::IpsContext* get_next() const;
74 
75     snort::IpsContextData* get_context_data(unsigned id) const;
76     void set_context_data(unsigned id, snort::IpsContextData*) const;
77 
78     unsigned idle_count() const;
79     unsigned busy_count() const;
80 
81 public:
82     snort::IpsContextChain non_flow_chain;
83 
84 private:
85     std::vector<snort::IpsContext*> idle;
86     std::vector<snort::IpsContext*> busy;
87     std::vector<snort::IpsContext*> contexts;
88 };
89 
90 #endif
91 
92