/src/openvswitch/lib/ovs-replay.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021, Red Hat, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #ifndef OVS_REPLAY_H |
18 | | #define OVS_REPLAY_H 1 |
19 | | |
20 | | /* |
21 | | * Library to work with 'replay' files. |
22 | | * |
23 | | * ovs_replay_file_open() should be used to open a new replay file. |
24 | | * 'replay' file contains records. If current state is OVS_REPLAY_WRITE, |
25 | | * files are opened in a write mode and new records could be written by |
26 | | * ovs_replay_write(). If current mode is OVS_REPLAY_READ, files are |
27 | | * opened in a read mode and records could be read with ovs_replay_read(). |
28 | | * |
29 | | * Each record has several fields: |
30 | | * <seqno> <len> [<data>] |
31 | | * |
32 | | * Here <seqno> is a global sequence number of the record, it is unique |
33 | | * across all the replay files. By comparing normalized version of this |
34 | | * number (ovs_replay_normalized_seqno()) with the current global sequence |
35 | | * number (ovs_replay_seqno()) users may detect if this record should be |
36 | | * replayed now. |
37 | | * |
38 | | * Non-normalized versions of seqno are used to distinguish 'read' and 'write' |
39 | | * records. 'read' records are records that corresponds to incoming events. |
40 | | * Only 'read' records contains <data>. 'write' records contains outgoing |
41 | | * events, i.e. stream_write() and contains only the size of outgoing message. |
42 | | * |
43 | | * For 'read' records, <len> is a size of a <data> stored in this record in |
44 | | * bytes. For 'write' records, it is a size of outgoing message, but there |
45 | | * is no <data>. If it contains negative value, it means that this record |
46 | | * holds some recorded error and no data available. |
47 | | */ |
48 | | |
49 | | #include <stdbool.h> |
50 | | #include <stdio.h> |
51 | | |
52 | | typedef FILE *replay_file_t; |
53 | | |
54 | | /* Replay state. */ |
55 | | enum ovs_replay_state { |
56 | | OVS_REPLAY_NONE, |
57 | | OVS_REPLAY_WRITE, |
58 | | OVS_REPLAY_READ, |
59 | | }; |
60 | | |
61 | | void ovs_replay_set_state(enum ovs_replay_state); |
62 | | enum ovs_replay_state ovs_replay_get_state(void); |
63 | | |
64 | | static inline bool |
65 | | ovs_replay_is_active(void) |
66 | 0 | { |
67 | 0 | return ovs_replay_get_state() != OVS_REPLAY_NONE; |
68 | 0 | } Unexecuted instantiation: unixctl.c:ovs_replay_is_active Unexecuted instantiation: uuid.c:ovs_replay_is_active Unexecuted instantiation: jsonrpc.c:ovs_replay_is_active Unexecuted instantiation: ovs-replay.c:ovs_replay_is_active Unexecuted instantiation: stream.c:ovs_replay_is_active Unexecuted instantiation: stream-unix.c:ovs_replay_is_active Unexecuted instantiation: stream-ssl.c:ovs_replay_is_active Unexecuted instantiation: stream-fd.c:ovs_replay_is_active Unexecuted instantiation: stream-replay.c:ovs_replay_is_active Unexecuted instantiation: stream-tcp.c:ovs_replay_is_active |
69 | | |
70 | | /* Returns 'true' if provided sequence number belongs to 'read' record. */ |
71 | | static inline bool |
72 | | ovs_replay_seqno_is_read(int seqno) |
73 | 0 | { |
74 | 0 | return seqno >= 0; |
75 | 0 | } Unexecuted instantiation: unixctl.c:ovs_replay_seqno_is_read Unexecuted instantiation: uuid.c:ovs_replay_seqno_is_read Unexecuted instantiation: jsonrpc.c:ovs_replay_seqno_is_read Unexecuted instantiation: ovs-replay.c:ovs_replay_seqno_is_read Unexecuted instantiation: stream.c:ovs_replay_seqno_is_read Unexecuted instantiation: stream-unix.c:ovs_replay_seqno_is_read Unexecuted instantiation: stream-ssl.c:ovs_replay_seqno_is_read Unexecuted instantiation: stream-fd.c:ovs_replay_seqno_is_read Unexecuted instantiation: stream-replay.c:ovs_replay_seqno_is_read Unexecuted instantiation: stream-tcp.c:ovs_replay_seqno_is_read |
76 | | |
77 | | /* Normalizes sequence number, so it can be used to compare with result of |
78 | | * ovs_replay_seqno(). */ |
79 | | static inline int |
80 | | ovs_replay_normalized_seqno(int seqno) |
81 | 0 | { |
82 | 0 | return seqno >= 0 ? seqno : -seqno; |
83 | 0 | } Unexecuted instantiation: unixctl.c:ovs_replay_normalized_seqno Unexecuted instantiation: uuid.c:ovs_replay_normalized_seqno Unexecuted instantiation: jsonrpc.c:ovs_replay_normalized_seqno Unexecuted instantiation: ovs-replay.c:ovs_replay_normalized_seqno Unexecuted instantiation: stream.c:ovs_replay_normalized_seqno Unexecuted instantiation: stream-unix.c:ovs_replay_normalized_seqno Unexecuted instantiation: stream-ssl.c:ovs_replay_normalized_seqno Unexecuted instantiation: stream-fd.c:ovs_replay_normalized_seqno Unexecuted instantiation: stream-replay.c:ovs_replay_normalized_seqno Unexecuted instantiation: stream-tcp.c:ovs_replay_normalized_seqno |
84 | | |
85 | | /* Locks the replay module. |
86 | | * Locking required to use ovs_replay_file_open() and ovs_replay_read(). */ |
87 | | void ovs_replay_lock(void); |
88 | | |
89 | | /* Unlocks the replay module. */ |
90 | | void ovs_replay_unlock(void); |
91 | | |
92 | | /* Returns current global replay sequence number. */ |
93 | | int ovs_replay_seqno(void); |
94 | | |
95 | | /* In write mode creates a new replay file to write stream replay. |
96 | | * In read mode opens an existing replay file. |
97 | | * |
98 | | * Requires replay being locked with ovs_replay_lock(). |
99 | | * |
100 | | * On success returns 0, 'f' points to the opened file. If current mode is |
101 | | * OVS_REPLAY_READ, sets 'seqno' to the sequence number of the first record in |
102 | | * the file. |
103 | | * |
104 | | * On failure returns positive errno. */ |
105 | | int ovs_replay_file_open(const char *name, replay_file_t *f, int *seqno); |
106 | | |
107 | | /* Closes replay file. */ |
108 | | void ovs_replay_file_close(replay_file_t f); |
109 | | |
110 | | /* Writes a new record of 'n' bytes from 'buffer' to a replay file. |
111 | | * 'is_read' should be true if the record belongs to 'read' operation |
112 | | * Depending on 'is_read', creates 'read' or 'write' record. 'write' records |
113 | | * contains only the size of a bufer ('n'). |
114 | | * If 'n' is negative, writes 'n' as an error status. |
115 | | * |
116 | | * On success returns 0. Otherwise, positive errno. */ |
117 | | int ovs_replay_write(replay_file_t f, const void *buffer, int n, bool is_read); |
118 | | |
119 | | /* Reads one record from a replay file to 'buffer'. 'buffer_size' should be |
120 | | * equal to the size of a memory available. |
121 | | * |
122 | | * On success, actual size of the read record will be set to 'len', 'seqno' |
123 | | * will be set to the sequence number of the next record in the file. If it |
124 | | * was the last record, sets 'seqno' to INT_MAX. |
125 | | * Negative 'len' means that record contained an error status. |
126 | | * |
127 | | * Depending on 'is_read', tries to read 'read' or 'write' record. For the |
128 | | * 'write' record, only 'len' and 'seqno' updated, no data read to 'buffer'. |
129 | | * |
130 | | * On success returns 0. Otherwise, positive errno. */ |
131 | | int ovs_replay_read(replay_file_t f, void *buffer, int buffer_size, |
132 | | int *len, int *seqno, bool is_read); |
133 | | |
134 | | /* Helpers for cmdline options. */ |
135 | | #define OVS_REPLAY_OPTION_ENUMS \ |
136 | | OPT_OVS_REPLAY_REC, \ |
137 | | OPT_OVS_REPLAY |
138 | | |
139 | | #define OVS_REPLAY_LONG_OPTIONS \ |
140 | | {"record", optional_argument, NULL, OPT_OVS_REPLAY_REC}, \ |
141 | | {"replay", optional_argument, NULL, OPT_OVS_REPLAY} |
142 | | |
143 | | #define OVS_REPLAY_OPTION_HANDLERS \ |
144 | | case OPT_OVS_REPLAY_REC: \ |
145 | | ovs_replay_set_state(OVS_REPLAY_WRITE); \ |
146 | | ovs_replay_set_dirname(optarg); \ |
147 | | break; \ |
148 | | \ |
149 | | case OPT_OVS_REPLAY: \ |
150 | | ovs_replay_set_state(OVS_REPLAY_READ); \ |
151 | | ovs_replay_set_dirname(optarg); \ |
152 | | break; |
153 | | |
154 | | #define OVS_REPLAY_CASES \ |
155 | | case OPT_OVS_REPLAY_REC: case OPT_OVS_REPLAY: |
156 | | |
157 | | /* Prints usage information. */ |
158 | | void ovs_replay_usage(void); |
159 | | |
160 | | /* Sets path to the directory where replay files should be stored. */ |
161 | | void ovs_replay_set_dirname(const char *new_dirname); |
162 | | |
163 | | #endif /* OVS_REPLAY_H */ |