/* * swlib.h: Switch configuration API (user space part) * * Copyright (C) 2008-2009 Felix Fietkau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * version 2.1 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * Usage of the library functions: The main datastructure for a switch is the struct switch_device To get started, you first need to use switch_connect() to probe for switches and allocate an instance of this struct. There are two possible usage modes: dev = switch_connect("eth0"); - this call will look for a switch registered for the linux device "eth0" and only allocate a switch_device for this particular switch. dev = switch_connect(NULL) - this will return one switch_device struct for each available switch. The switch_device structs are chained with by ->next pointer Then to query a switch for all available attributes, use: swlib_scan(dev); All allocated datastructures for the switch_device struct can be freed with swlib_free(dev); or swlib_free_all(dev); The latter traverses a whole chain of switch_device structs and frees them all Switch attributes (struct switch_attr) are divided into three groups: dev->ops: - global settings dev->port_ops: - per-port settings dev->vlan_ops: - per-vlan settings switch_lookup_attr() is a small helper function to locate attributes by name. switch_set_attr() and switch_get_attr() can alter or request the values of attributes. Usage of the switch_attr struct: ->atype: attribute group, one of: - SWLIB_ATTR_GROUP_GLOBAL - SWLIB_ATTR_GROUP_VLAN - SWLIB_ATTR_GROUP_PORT ->id: identifier for the attribute ->type: data type, one of: - SWITCH_TYPE_INT - SWITCH_TYPE_STRING - SWITCH_TYPE_PORT ->name: short name of the attribute ->description: longer description ->next: pointer to the next attribute of the current group Usage of the switch_val struct: When setting attributes, following members of the struct switch_val need to be set up: ->len (for attr->type == SWITCH_TYPE_PORT) ->port_vlan: - port number (for attr->atype == SWLIB_ATTR_GROUP_PORT), or: - vlan number (for attr->atype == SWLIB_ATTR_GROUP_VLAN) ->value.i (for attr->type == SWITCH_TYPE_INT) ->value.s (for attr->type == SWITCH_TYPE_STRING) - owned by the caller, not stored in the library internally ->value.ports (for attr->type == SWITCH_TYPE_PORT) - must point to an array of at lest val->len * sizeof(struct switch_port) When getting string attributes, val->value.s must be freed by the caller When getting port list attributes, an internal static buffer is used, which changes from call to call. */ #ifndef __SWLIB_H #define __SWLIB_H enum swlib_attr_group { SWLIB_ATTR_GROUP_GLOBAL, SWLIB_ATTR_GROUP_VLAN, SWLIB_ATTR_GROUP_PORT, }; enum swlib_port_flags { SWLIB_PORT_FLAG_TAGGED = (1 << 0), }; enum swlib_link_flags { SWLIB_LINK_FLAG_EEE_100BASET = (1 << 0), SWLIB_LINK_FLAG_EEE_1000BASET = (1 << 1), }; struct switch_dev; struct switch_attr; struct switch_port; struct switch_port_map; struct switch_port_link; struct switch_val; struct uci_package; struct switch_dev { int id; char dev_name[IFNAMSIZ]; char *name; char *alias; int ports; int vlans; int cpu_port; struct switch_attr *ops; struct switch_attr *port_ops; struct switch_attr *vlan_ops; struct switch_portmap *maps; struct switch_dev *next; void *priv; }; struct switch_val { struct switch_attr *attr; int len; int err; int port_vlan; union { char *s; int i; struct switch_port *ports; struct switch_port_link *link; } value; }; struct switch_attr { struct switch_dev *dev; int atype; int id; int type; char *name; char *description; struct switch_attr *next; }; struct switch_port { unsigned int id; unsigned int flags; }; struct switch_portmap { unsigned int virt; char *segment; }; struct switch_port_link { int link:1; int duplex:1; int aneg:1; int tx_flow:1; int rx_flow:1; int speed; /* in ethtool adv_t format */ uint32_t eee; }; /** * swlib_list: list all switches */ void swlib_list(void); /** * swlib_print_portmap: get portmap * @dev: switch device struct */ void swlib_print_portmap(struct switch_dev *dev, char *segment); /** * swlib_connect: connect to the switch through netlink * @name: name of the ethernet interface, * * if name is NULL, it connect and builds a chain of all switches */ struct switch_dev *swlib_connect(const char *name); /** * swlib_free: free all dynamically allocated data for the switch connection * @dev: switch device struct * * all members of a switch device chain (generated by swlib_connect(NULL)) * must be freed individually */ void swlib_free(struct switch_dev *dev); /** * swlib_free_all: run swlib_free on all devices in the chain * @dev: switch device struct */ void swlib_free_all(struct switch_dev *dev); /** * swlib_scan: probe the switch driver for available commands/attributes * @dev: switch device struct */ int swlib_scan(struct switch_dev *dev); /** * swlib_lookup_attr: look up a switch attribute * @dev: switch device struct * @type: global, port or vlan * @name: name of the attribute */ struct switch_attr *swlib_lookup_attr(struct switch_dev *dev, enum swlib_attr_group atype, const char *name); /** * swlib_set_attr: set the value for an attribute * @dev: switch device struct * @attr: switch attribute struct * @val: attribute value pointer * returns 0 on success */ int swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val); /** * swlib_set_attr_string: set the value for an attribute with type conversion * @dev: switch device struct * @attr: switch attribute struct * @port_vlan: port or vlan (if applicable) * @str: string value * returns 0 on success */ int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *attr, int port_vlan, const char *str); /** * swlib_get_attr: get the value for an attribute * @dev: switch device struct * @attr: switch attribute struct * @val: attribute value pointer * returns 0 on success * for string attributes, the result string must be freed by the caller */ int swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val); /** * swlib_apply_from_uci: set up the switch from a uci configuration * @dev: switch device struct * @p: uci package which contains the desired global config */ int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p); #endif f='#n99'>99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/******************************************************************************/
/*                                                                            */
/* Broadcom BCM5700 Linux Network Driver, Copyright (c) 2000 - 2004 Broadcom  */
/* Corporation.                                                               */
/* All rights reserved.                                                       */
/*                                                                            */
/* This program is free software; you can redistribute it and/or modify       */
/* it under the terms of the GNU General Public License as published by       */
/* the Free Software Foundation, located in the file LICENSE.                 */
/*                                                                            */
/* Queue functions.                                                           */
/*    void          QQ_InitQueue(PQQ_CONTAINER pQueue)                        */
/*    char          QQ_Full(PQQ_CONTAINER pQueue)                             */
/*    char          QQ_Empty(PQQ_CONTAINER pQueue)                            */
/*    unsigned int QQ_GetSize(PQQ_CONTAINER pQueue)                          */
/*    unsigned int QQ_GetEntryCnt(PQQ_CONTAINER pQueue)                      */
/*    char          QQ_PushHead(PQQ_CONTAINER pQueue, PQQ_ENTRY pEntry)       */
/*    char          QQ_PushTail(PQQ_CONTAINER pQueue, PQQ_ENTRY pEntry)       */
/*    PQQ_ENTRY     QQ_PopHead(PQQ_CONTAINER pQueue)                          */
/*    PQQ_ENTRY     QQ_PopTail(PQQ_CONTAINER pQueue)                          */
/*    PQQ_ENTRY     QQ_GetHead(PQQ_CONTAINER pQueue, unsigned int Idx)       */
/*    PQQ_ENTRY     QQ_GetTail(PQQ_CONTAINER pQueue, unsigned int Idx)       */
/*                                                                            */
/*                                                                            */
/* History:                                                                   */
/*    02/25/00 Hav Khauv        Initial version.                              */
/******************************************************************************/

#ifndef BCM_QUEUE_H
#define BCM_QUEUE_H



/******************************************************************************/
/* Queue definitions. */
/******************************************************************************/

/* Entry for queueing. */
typedef void *PQQ_ENTRY;


/* Queue header -- base type. */
typedef struct {
    unsigned int Head;
    unsigned int Tail;
    unsigned int Size;
    MM_ATOMIC_T EntryCnt;
    PQQ_ENTRY Array[1];
} QQ_CONTAINER, *PQQ_CONTAINER;


/* Declare queue type macro. */
#define DECLARE_QUEUE_TYPE(_QUEUE_TYPE, _QUEUE_SIZE)            \
                                                                \
    typedef struct {                                            \
        QQ_CONTAINER Container;                                 \
        PQQ_ENTRY EntryBuffer[_QUEUE_SIZE];                     \
    } _QUEUE_TYPE, *P##_QUEUE_TYPE



/******************************************************************************/
/* Compilation switches. */
/******************************************************************************/

#if DBG
#undef QQ_NO_OVERFLOW_CHECK
#undef QQ_NO_UNDERFLOW_CHECK
#endif /* DBG */

#ifdef QQ_USE_MACROS
/* notdone */
#else

#ifdef QQ_NO_INLINE
#define __inline
#endif /* QQ_NO_INLINE */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static void 
QQ_InitQueue(
PQQ_CONTAINER pQueue,
unsigned int QueueSize) {
    pQueue->Head = 0;
    pQueue->Tail = 0;
    pQueue->Size = QueueSize+1;
    MM_ATOMIC_SET(&pQueue->EntryCnt, 0);
} /* QQ_InitQueue */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static char 
QQ_Full(
PQQ_CONTAINER pQueue) {
    unsigned int NewHead;

    NewHead = (pQueue->Head + 1) % pQueue->Size;

    return(NewHead == pQueue->Tail);
} /* QQ_Full */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static char 
QQ_Empty(
PQQ_CONTAINER pQueue) {
    return(pQueue->Head == pQueue->Tail);
} /* QQ_Empty */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static unsigned int 
QQ_GetSize(
PQQ_CONTAINER pQueue) {
    return pQueue->Size;
} /* QQ_GetSize */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static unsigned int 
QQ_GetEntryCnt(
PQQ_CONTAINER pQueue) {
    return MM_ATOMIC_READ(&pQueue->EntryCnt);
} /* QQ_GetEntryCnt */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/*    TRUE entry was added successfully.                                      */
/*    FALSE queue is full.                                                    */
/******************************************************************************/
__inline static char 
QQ_PushHead(
PQQ_CONTAINER pQueue, 
PQQ_ENTRY pEntry) {
    unsigned int Head;

    Head = (pQueue->Head + 1) % pQueue->Size;

#if !defined(QQ_NO_OVERFLOW_CHECK)
    if(Head == pQueue->Tail) {
        return 0;
    } /* if */
#endif /* QQ_NO_OVERFLOW_CHECK */

    pQueue->Array[pQueue->Head] = pEntry;
    MM_WMB();
    pQueue->Head = Head;
    MM_ATOMIC_INC(&pQueue->EntryCnt);

    return -1;
} /* QQ_PushHead */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/*    TRUE entry was added successfully.                                      */
/*    FALSE queue is full.                                                    */
/******************************************************************************/
__inline static char 
QQ_PushTail(
PQQ_CONTAINER pQueue,
PQQ_ENTRY pEntry) {
    unsigned int Tail;

    Tail = pQueue->Tail;
    if(Tail == 0) {
        Tail = pQueue->Size;
    } /* if */
    Tail--;

#if !defined(QQ_NO_OVERFLOW_CHECK)
    if(Tail == pQueue->Head) {
        return 0;
    } /* if */
#endif /* QQ_NO_OVERFLOW_CHECK */

    pQueue->Array[Tail] = pEntry;
    MM_WMB();
    pQueue->Tail = Tail;
    MM_ATOMIC_INC(&pQueue->EntryCnt);

    return -1;
} /* QQ_PushTail */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static PQQ_ENTRY
QQ_PopHead(
PQQ_CONTAINER pQueue) {
    unsigned int Head;
    unsigned int Tail;
    PQQ_ENTRY Entry;

    Head = pQueue->Head;
    Tail = pQueue->Tail;

    MM_MB();
#if !defined(QQ_NO_UNDERFLOW_CHECK)
    if(Head == Tail) {
        return (PQQ_ENTRY) 0;
    } /* if */
#endif /* QQ_NO_UNDERFLOW_CHECK */

    if(Head == 0) {
        Head = pQueue->Size;
    } /* if */
    Head--;

    Entry = pQueue->Array[Head];
    MM_MB();
    pQueue->Head = Head;
    MM_ATOMIC_DEC(&pQueue->EntryCnt);

    return Entry;
} /* QQ_PopHead */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static PQQ_ENTRY
QQ_PopTail(
PQQ_CONTAINER pQueue) {
    unsigned int Head;
    unsigned int Tail;
    PQQ_ENTRY Entry;

    Head = pQueue->Head;
    Tail = pQueue->Tail;

    MM_MB();
#if !defined(QQ_NO_UNDERFLOW_CHECK)
    if(Tail == Head) {
        return (PQQ_ENTRY) 0;
    } /* if */
#endif /* QQ_NO_UNDERFLOW_CHECK */

    Entry = pQueue->Array[Tail];
    MM_MB();
    pQueue->Tail = (Tail + 1) % pQueue->Size;
    MM_ATOMIC_DEC(&pQueue->EntryCnt);

    return Entry;
} /* QQ_PopTail */



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static PQQ_ENTRY
QQ_GetHead(
    PQQ_CONTAINER pQueue,
    unsigned int Idx)
{
    if(Idx >= (unsigned int) MM_ATOMIC_READ(&pQueue->EntryCnt))
    {
        return (PQQ_ENTRY) 0;
    }

    if(pQueue->Head > Idx)
    {
        Idx = pQueue->Head - Idx;
    }
    else
    {
        Idx = pQueue->Size - (Idx - pQueue->Head);
    }
    Idx--;

    return pQueue->Array[Idx];
}



/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
__inline static PQQ_ENTRY
QQ_GetTail(
    PQQ_CONTAINER pQueue,
    unsigned int Idx)
{
    if(Idx >= (unsigned int) MM_ATOMIC_READ(&pQueue->EntryCnt))
    {
        return (PQQ_ENTRY) 0;
    }

    Idx += pQueue->Tail;
    if(Idx >= pQueue->Size)
    {
        Idx = Idx - pQueue->Size;
    }

    return pQueue->Array[Idx];
}

#endif /* QQ_USE_MACROS */



#endif /* QUEUE_H */