Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/core.h: Include file for TIPC global declarations |
| 3 | * |
Per Liden | 593a5f2 | 2006-01-11 19:14:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 2005-2006, Ericsson AB |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 5 | * Copyright (c) 2005, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 19 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #ifndef _TIPC_CORE_H |
| 38 | #define _TIPC_CORE_H |
| 39 | |
| 40 | #include <net/tipc/tipc.h> |
| 41 | #include <linux/types.h> |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/errno.h> |
| 44 | #include <linux/mm.h> |
| 45 | #include <linux/timer.h> |
| 46 | #include <linux/string.h> |
| 47 | #include <asm/uaccess.h> |
| 48 | #include <linux/interrupt.h> |
| 49 | #include <asm/atomic.h> |
| 50 | #include <asm/hardirq.h> |
| 51 | #include <linux/netdevice.h> |
| 52 | #include <linux/in.h> |
| 53 | #include <linux/list.h> |
| 54 | #include <linux/vmalloc.h> |
| 55 | |
| 56 | /* |
| 57 | * TIPC debugging code |
| 58 | */ |
| 59 | |
| 60 | #define assert(i) BUG_ON(!(i)) |
| 61 | |
| 62 | struct tipc_msg; |
| 63 | extern struct print_buf *CONS, *LOG; |
| 64 | extern struct print_buf *TEE(struct print_buf *, struct print_buf *); |
| 65 | void msg_print(struct print_buf*,struct tipc_msg *,const char*); |
| 66 | void tipc_printf(struct print_buf *, const char *fmt, ...); |
| 67 | void tipc_dump(struct print_buf*,const char *fmt, ...); |
| 68 | |
| 69 | #ifdef CONFIG_TIPC_DEBUG |
| 70 | |
| 71 | /* |
| 72 | * TIPC debug support included: |
| 73 | * - system messages are printed to TIPC_OUTPUT print buffer |
| 74 | * - debug messages are printed to DBG_OUTPUT print buffer |
| 75 | */ |
| 76 | |
| 77 | #define err(fmt, arg...) tipc_printf(TIPC_OUTPUT, KERN_ERR "TIPC: " fmt, ## arg) |
| 78 | #define warn(fmt, arg...) tipc_printf(TIPC_OUTPUT, KERN_WARNING "TIPC: " fmt, ## arg) |
| 79 | #define info(fmt, arg...) tipc_printf(TIPC_OUTPUT, KERN_NOTICE "TIPC: " fmt, ## arg) |
| 80 | |
| 81 | #define dbg(fmt, arg...) do {if (DBG_OUTPUT) tipc_printf(DBG_OUTPUT, fmt, ## arg);} while(0) |
| 82 | #define msg_dbg(msg, txt) do {if (DBG_OUTPUT) msg_print(DBG_OUTPUT, msg, txt);} while(0) |
| 83 | #define dump(fmt, arg...) do {if (DBG_OUTPUT) tipc_dump(DBG_OUTPUT, fmt, ##arg);} while(0) |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * By default, TIPC_OUTPUT is defined to be system console and TIPC log buffer, |
| 88 | * while DBG_OUTPUT is the null print buffer. These defaults can be changed |
| 89 | * here, or on a per .c file basis, by redefining these symbols. The following |
| 90 | * print buffer options are available: |
| 91 | * |
| 92 | * NULL : Output to null print buffer (i.e. print nowhere) |
| 93 | * CONS : Output to system console |
| 94 | * LOG : Output to TIPC log buffer |
| 95 | * &buf : Output to user-defined buffer (struct print_buf *) |
| 96 | * TEE(&buf_a,&buf_b) : Output to two print buffers (eg. TEE(CONS,LOG) ) |
| 97 | */ |
| 98 | |
| 99 | #ifndef TIPC_OUTPUT |
| 100 | #define TIPC_OUTPUT TEE(CONS,LOG) |
| 101 | #endif |
| 102 | |
| 103 | #ifndef DBG_OUTPUT |
| 104 | #define DBG_OUTPUT NULL |
| 105 | #endif |
| 106 | |
| 107 | #else |
| 108 | |
| 109 | #ifndef DBG_OUTPUT |
| 110 | #define DBG_OUTPUT NULL |
| 111 | #endif |
| 112 | |
| 113 | /* |
| 114 | * TIPC debug support not included: |
| 115 | * - system messages are printed to system console |
| 116 | * - debug messages are not printed |
| 117 | */ |
| 118 | |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 119 | #define err(fmt, arg...) printk(KERN_ERR "TIPC: " fmt , ## arg) |
| 120 | #define info(fmt, arg...) printk(KERN_INFO "TIPC: " fmt , ## arg) |
| 121 | #define warn(fmt, arg...) printk(KERN_WARNING "TIPC: " fmt , ## arg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 122 | |
| 123 | #define dbg(fmt, arg...) do {} while (0) |
| 124 | #define msg_dbg(msg,txt) do {} while (0) |
| 125 | #define dump(fmt,arg...) do {} while (0) |
| 126 | |
| 127 | #endif |
| 128 | |
| 129 | |
| 130 | /* |
| 131 | * TIPC-specific error codes |
| 132 | */ |
| 133 | |
| 134 | #define ELINKCONG EAGAIN /* link congestion <=> resource unavailable */ |
| 135 | |
| 136 | /* |
| 137 | * Global configuration variables |
| 138 | */ |
| 139 | |
| 140 | extern u32 tipc_own_addr; |
| 141 | extern int tipc_max_zones; |
| 142 | extern int tipc_max_clusters; |
| 143 | extern int tipc_max_nodes; |
| 144 | extern int tipc_max_slaves; |
| 145 | extern int tipc_max_ports; |
| 146 | extern int tipc_max_subscriptions; |
| 147 | extern int tipc_max_publications; |
| 148 | extern int tipc_net_id; |
| 149 | extern int tipc_remote_management; |
| 150 | |
| 151 | /* |
| 152 | * Other global variables |
| 153 | */ |
| 154 | |
| 155 | extern int tipc_mode; |
| 156 | extern int tipc_random; |
| 157 | extern const char tipc_alphabet[]; |
| 158 | extern atomic_t tipc_user_count; |
| 159 | |
| 160 | |
| 161 | /* |
| 162 | * Routines available to privileged subsystems |
| 163 | */ |
| 164 | |
| 165 | extern int start_core(void); |
| 166 | extern void stop_core(void); |
| 167 | extern int start_net(void); |
| 168 | extern void stop_net(void); |
| 169 | |
| 170 | static inline int delimit(int val, int min, int max) |
| 171 | { |
| 172 | if (val > max) |
| 173 | return max; |
| 174 | if (val < min) |
| 175 | return min; |
| 176 | return val; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* |
| 181 | * TIPC timer and signal code |
| 182 | */ |
| 183 | |
| 184 | typedef void (*Handler) (unsigned long); |
| 185 | |
| 186 | u32 k_signal(Handler routine, unsigned long argument); |
| 187 | |
| 188 | /** |
| 189 | * k_init_timer - initialize a timer |
| 190 | * @timer: pointer to timer structure |
| 191 | * @routine: pointer to routine to invoke when timer expires |
| 192 | * @argument: value to pass to routine when timer expires |
| 193 | * |
| 194 | * Timer must be initialized before use (and terminated when no longer needed). |
| 195 | */ |
| 196 | |
| 197 | static inline void k_init_timer(struct timer_list *timer, Handler routine, |
| 198 | unsigned long argument) |
| 199 | { |
| 200 | dbg("initializing timer %p\n", timer); |
| 201 | init_timer(timer); |
| 202 | timer->function = routine; |
| 203 | timer->data = argument; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * k_start_timer - start a timer |
| 208 | * @timer: pointer to timer structure |
| 209 | * @msec: time to delay (in ms) |
| 210 | * |
| 211 | * Schedules a previously initialized timer for later execution. |
| 212 | * If timer is already running, the new timeout overrides the previous request. |
| 213 | * |
| 214 | * To ensure the timer doesn't expire before the specified delay elapses, |
| 215 | * the amount of delay is rounded up when converting to the jiffies |
| 216 | * then an additional jiffy is added to account for the fact that |
| 217 | * the starting time may be in the middle of the current jiffy. |
| 218 | */ |
| 219 | |
| 220 | static inline void k_start_timer(struct timer_list *timer, unsigned long msec) |
| 221 | { |
| 222 | dbg("starting timer %p for %u\n", timer, msec); |
| 223 | mod_timer(timer, jiffies + msecs_to_jiffies(msec) + 1); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * k_cancel_timer - cancel a timer |
| 228 | * @timer: pointer to timer structure |
| 229 | * |
| 230 | * Cancels a previously initialized timer. |
| 231 | * Can be called safely even if the timer is already inactive. |
| 232 | * |
| 233 | * WARNING: Must not be called when holding locks required by the timer's |
| 234 | * timeout routine, otherwise deadlock can occur on SMP systems! |
| 235 | */ |
| 236 | |
| 237 | static inline void k_cancel_timer(struct timer_list *timer) |
| 238 | { |
| 239 | dbg("cancelling timer %p\n", timer); |
| 240 | del_timer_sync(timer); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * k_term_timer - terminate a timer |
| 245 | * @timer: pointer to timer structure |
| 246 | * |
| 247 | * Prevents further use of a previously initialized timer. |
| 248 | * |
| 249 | * WARNING: Caller must ensure timer isn't currently running. |
| 250 | * |
| 251 | * (Do not "enhance" this routine to automatically cancel an active timer, |
| 252 | * otherwise deadlock can arise when a timeout routine calls k_term_timer.) |
| 253 | */ |
| 254 | |
| 255 | static inline void k_term_timer(struct timer_list *timer) |
| 256 | { |
| 257 | dbg("terminating timer %p\n", timer); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /* |
| 262 | * TIPC message buffer code |
| 263 | * |
| 264 | * TIPC message buffer headroom leaves room for 14 byte Ethernet header, |
| 265 | * while ensuring TIPC header is word aligned for quicker access |
| 266 | */ |
| 267 | |
| 268 | #define BUF_HEADROOM 16u |
| 269 | |
| 270 | struct tipc_skb_cb { |
| 271 | void *handle; |
| 272 | }; |
| 273 | |
| 274 | #define TIPC_SKB_CB(__skb) ((struct tipc_skb_cb *)&((__skb)->cb[0])) |
| 275 | |
| 276 | |
| 277 | static inline struct tipc_msg *buf_msg(struct sk_buff *skb) |
| 278 | { |
| 279 | return (struct tipc_msg *)skb->data; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * buf_acquire - creates a TIPC message buffer |
| 284 | * @size: message size (including TIPC header) |
| 285 | * |
| 286 | * Returns a new buffer. Space is reserved for a data link header. |
| 287 | */ |
| 288 | |
| 289 | static inline struct sk_buff *buf_acquire(u32 size) |
| 290 | { |
| 291 | struct sk_buff *skb; |
| 292 | unsigned int buf_size = (BUF_HEADROOM + size + 3) & ~3u; |
| 293 | |
| 294 | skb = alloc_skb(buf_size, GFP_ATOMIC); |
| 295 | if (skb) { |
| 296 | skb_reserve(skb, BUF_HEADROOM); |
| 297 | skb_put(skb, size); |
| 298 | skb->next = NULL; |
| 299 | } |
| 300 | return skb; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * buf_discard - frees a TIPC message buffer |
| 305 | * @skb: message buffer |
| 306 | * |
| 307 | * Frees a new buffer. If passed NULL, just returns. |
| 308 | */ |
| 309 | |
| 310 | static inline void buf_discard(struct sk_buff *skb) |
| 311 | { |
| 312 | if (likely(skb != NULL)) |
| 313 | kfree_skb(skb); |
| 314 | } |
| 315 | |
| 316 | #endif |