1 | /* |
---|
2 | * remote_buf.h - Remotely accessible, circular buffer definition. |
---|
3 | * |
---|
4 | * Authors : |
---|
5 | * Alain Greiner (2016,2017,2018,2019,2020) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MHH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef _REMOTE_BUFFER_H_ |
---|
26 | #define _REMOTE_BUFFER_H_ |
---|
27 | |
---|
28 | #include <kernel_config.h> |
---|
29 | #include <hal_kernel_types.h> |
---|
30 | |
---|
31 | /************************************************************************************ |
---|
32 | * This structure and the associated access functions define a remotely accessible, |
---|
33 | * kernel buffer, handled as a single-reader & single-writer FIFO. |
---|
34 | * Each buffer slot contains one byte, and the number of slots is dynamically |
---|
35 | * defined by an argument of the "remote_buf_create()" function. |
---|
36 | * The "put()" and "get()" access functions defined below move another buffer, |
---|
37 | * (can be in kernel or in user space) to/from this circular kernel buffer. |
---|
38 | * This structure is NOT protected by a lock, as the only shared variable is the |
---|
39 | * "sts" variable, that is updated by hal_remote_atomic_add() access primitives. |
---|
40 | * It is used by to implement the socket receive buffer. |
---|
41 | * - the "ptw" field defines the first empty slot (for write). |
---|
42 | * - the "ptr" field defines the first non empty slot (for read). |
---|
43 | * - the "sts" field defines the total number of non empty slots. |
---|
44 | ***********************************************************************************/ |
---|
45 | |
---|
46 | typedef struct remote_buf_s |
---|
47 | { |
---|
48 | uint32_t size; /*! number of slots in data buffer */ |
---|
49 | uint32_t ptw; /*! first empty slot index */ |
---|
50 | uint32_t ptr; /*! first non-empty slot index */ |
---|
51 | uint32_t sts; /*! current number of non empty slots */ |
---|
52 | uint8_t * data; /*! local pointer on local data buffer */ |
---|
53 | } |
---|
54 | remote_buf_t; |
---|
55 | |
---|
56 | /************************************************************************************ |
---|
57 | * This function initializes a remote_buf descriptor identified by the <buf_xp> |
---|
58 | * argument. It allocates memory for the data, as defined by the <size> argument, |
---|
59 | * and initializes the buffer as empty. It must be called by a local thread. |
---|
60 | ************************************************************************************ |
---|
61 | * @ buf_xp : [in] extended pointer on buffer descriptor. |
---|
62 | * @ size : [in] number of bytes in buffer. |
---|
63 | * @ return 0 if success / return -1 if failure (no memory). |
---|
64 | ***********************************************************************************/ |
---|
65 | error_t remote_buf_create( xptr_t buf_xp, |
---|
66 | uint32_t size ); |
---|
67 | |
---|
68 | /************************************************************************************ |
---|
69 | * This function releases memory allocated for the data buffer of a remote-buf |
---|
70 | * descriptor identified by the <buf_xp> argument. |
---|
71 | * It must be called by a local thread. |
---|
72 | ************************************************************************************ |
---|
73 | * @ buf_xp : [in] extended pointer on buffer descriptor. |
---|
74 | ***********************************************************************************/ |
---|
75 | void remote_buf_destroy( xptr_t buf_xp ); |
---|
76 | |
---|
77 | /************************************************************************************ |
---|
78 | * This function initialises as empty a buffer identified by the <buf_xp> argument. |
---|
79 | * It can be called by a thread runing in any cluster. |
---|
80 | ************************************************************************************ |
---|
81 | * @ buf_xp : [in] extended pointer on remote buffer descriptor. |
---|
82 | ***********************************************************************************/ |
---|
83 | void remote_buf_reset( xptr_t buf_xp ); |
---|
84 | |
---|
85 | /************************************************************************************ |
---|
86 | * This function moves <nbytes> from a remote_buf_t identified by the <buf_xp> |
---|
87 | * argument to an user buffer identified by the <u_buf> argument. |
---|
88 | * It uses the hal_copy_to_uspace() function, and updates "sts" and "ptr". |
---|
89 | * The calling function is supposed to previously check the buffer status. |
---|
90 | * It can be called by a thread runing in any cluster. |
---|
91 | ************************************************************************************ |
---|
92 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
93 | * @ u_buf : [in] pointer on destination user buffer in user space. |
---|
94 | * @ nbytes : [in] number of bytes to move. |
---|
95 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
96 | ***********************************************************************************/ |
---|
97 | error_t remote_buf_get_to_user( xptr_t buf_xp, |
---|
98 | uint8_t * u_buf, |
---|
99 | uint32_t nbytes ); |
---|
100 | |
---|
101 | /************************************************************************************ |
---|
102 | * This function moves <nbytes> from a remote_buf_t identified by the <buf_xp> |
---|
103 | * argument to a local kernel buffer identified by the <k_buf> argument. |
---|
104 | * It uses an hal_remote_memcpy() function, and updates the "sts" and "ptr" fields. |
---|
105 | * The calling function is supposed to previously check the buffer status. |
---|
106 | * It can be called by a thread runing in any cluster. |
---|
107 | ************************************************************************************ |
---|
108 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
109 | * @ k_buf : [in] local pointer on local destination kernel buffer. |
---|
110 | * @ nbytes : [in] number of bytes to move. |
---|
111 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
112 | ***********************************************************************************/ |
---|
113 | error_t remote_buf_get_to_kernel( xptr_t buf_xp, |
---|
114 | uint8_t * k_buf, |
---|
115 | uint32_t nbytes ); |
---|
116 | |
---|
117 | /************************************************************************************ |
---|
118 | * This function moves <nbytes> from an user buffer identified by the <u_buf> |
---|
119 | * argument to a remote_buf_t identified by the <buf_xp> argument. |
---|
120 | * It uses the hal_copy_from_uspace() function, and updates "sts" and "ptw".. |
---|
121 | * The calling function is supposed to previously check the buffer status. |
---|
122 | * It can be called by a thread runing in any cluster. |
---|
123 | ************************************************************************************ |
---|
124 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
125 | * @ u_buf : [in] pointer on source user buffer in user space. |
---|
126 | * @ nbytes : [in] number of bytes to move. |
---|
127 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
128 | ***********************************************************************************/ |
---|
129 | error_t remote_buf_put_from_user( xptr_t buf_xp, |
---|
130 | uint8_t * u_buf, |
---|
131 | uint32_t nbytes ); |
---|
132 | |
---|
133 | /************************************************************************************ |
---|
134 | * This function moves <nbytes> from a local kernel buffer identified by the <k_buf> |
---|
135 | * argument to a remote_buf_t identified by the <buf_xp> argument. |
---|
136 | * It uses an hal_remote_memcpy() function, and updates the "sts" and "ptw" fields. |
---|
137 | * The calling function is supposed to previously check the buffer status. |
---|
138 | * It can be called by a thread runing in any cluster. |
---|
139 | ************************************************************************************ |
---|
140 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
141 | * @ k_buf : [in] local pointer on local source kernel buffer. |
---|
142 | * @ nbytes : [in] number of bytes to move. |
---|
143 | * @ return 0 if success / return -1 if not enough bytes in buffer. |
---|
144 | ***********************************************************************************/ |
---|
145 | error_t remote_buf_put_from_kernel( xptr_t buf_xp, |
---|
146 | uint8_t * k_buf, |
---|
147 | uint32_t nbytes ); |
---|
148 | |
---|
149 | /************************************************************************************ |
---|
150 | * This function returns the current number of bytes stored in buffer. |
---|
151 | ************************************************************************************ |
---|
152 | * @ buf_xp : [in] extended pointer pointer on remote buffer descriptor. |
---|
153 | * @ return current number of bytes in buffer. |
---|
154 | ***********************************************************************************/ |
---|
155 | uint32_t remote_buf_status( xptr_t buf_xp ); |
---|
156 | |
---|
157 | #endif /* _REMOTE_BUFFER_H_ */ |
---|