/* * \brief MAC-address allocator * \author Stefan Kalkowski * \date 2010-08-25 */ /* * Copyright (C) 2010-2017 Genode Labs GmbH * * This file is part of the Genode OS framework, which is distributed * under the terms of the GNU Affero General Public License version 3. */ #ifndef _MAC_ALLOCATOR_H_ #define _MAC_ALLOCATOR_H_ /* Genode includes */ #include #include namespace Net { /** * The MAC allocator is used to administer MAC addresses for * NIC session clients. */ class Mac_allocator { private: /* limit available MAC addresses to one byte range */ enum { MSB_MAX = 0xFF }; /* signals, whether most significant byte is in use */ typedef struct { unsigned used : 1; } Msb; Msb _msbs[MSB_MAX]; /* bitfield of MSBs */ public: class Alloc_failed : Genode::Exception {}; /** * Reference MAC address * * We take the range 02:02:02:02:02:XX for our MAC address * allocator, it's likely, that we will have no clashes here. * (e.g. Linux uses 02:00... for its tap-devices.) */ Mac_address mac_addr_base { Mac_address(0x02) }; Mac_allocator() { Genode::memset(&_msbs, 0, sizeof(_msbs)); } /** * Allocates a new MAC address. * * \throws Alloc_failed if no more MAC addresses are available. * \return MAC address */ Mac_address alloc() { for (int i=0; i < MSB_MAX; i++) { if (!_msbs[i].used) { _msbs[i].used = 1; Mac_address mac = mac_addr_base; mac.addr[5] = i; return mac; } } throw Alloc_failed(); } /** * Frees a formerly allocated MAC address. */ void free(Mac_address mac) { _msbs[(unsigned)mac.addr[5]].used = 0; } }; } #endif /* _MAC_ALLOCATOR_H_ */