Title: udp_port / udp_port_table[]


For the following example inet.conf file:

eth0 DP8390 0 { default; };
psip1;

there are 2 physical udp ports. Port 0 corresponds to the ethernet device whose corresponding udp device is /dev/udp0 and port 1 corresponds to the psip device whose corresponding udp device is /dev/udp1.

For this configuration file, udp_port_table[] will have two elements, udp_port_table[0] and udp_port_table[1]. udp_init() initializes these two elements to indicate they are unused and then calls udp_main(). udp_main(), in turn, calls ip_open(), which claims an element in ip_fd_table[] (in other words, an "ip file descriptor" is opened). In this way, the udp code and the ip code are interconnected. Note that there is a 1:1 relationship between udp_port_table[] elements and ip_fd_table[] elements. What this means is that a udp device (e.g., /dev/udp0) will use the same ip file descriptor (i.e., the same element of ip_fd_table[]) to handle every request (read, write, and i/o). On the other hand, there is a one-to-many relationship between udp ports and udp file descriptors.



Each element of udp_port_table[] is of type udp_port_t:

typedef struct udp_port

{
int up_flags;
int up_state;
int up_ipfd;
int up_ipdev;
acc_t *up_wr_pack;
ipaddr_t up_ipaddr;
struct udp_fd *up_next_fd;
struct udp_fd *up_write_fd;
struct udp_fd *up_port_any;
struct udp_fd *up_port_hash[UDP_PORT_HASH_NR];
} udp_port_t;

int up_flags:

#define UPF_EMPTY 0x0
0061039 #define UPF_WRITE_IP 0x1
0061040 #define UPF_WRITE_SP 0x2
0061041 #define UPF_READ_IP 0x4
0061042 #define UPF_READ_SP 0x8
0061043 #define UPF_SUSPEND 0x10
0061044 #define UPF_MORE2WRITE 0x20

Most of these flags are meaningless.

int up_state:

#define UPS_EMPTY 0
0061047 #define UPS_SETPROTO 1
0061048 #define UPS_GETCONF 2
0061049 #define UPS_MAIN 3
0061050 #define UPS_ERROR 4

During initialization, udp_main() is called to initialize the udp port. If the underlying ip port has been already configured, the udp port will be successfully initialized and the udp port's state will become UPS_MAIN, its normal operational state.

int up_ipfd:

As described above, ip_open() is called to open an ip file descriptor. up_ipfd is then set to this file descriptor (i.e., the index of the claimed element in ip_fd_table[]). In this way, the udp code and the ip code are linked. For example, immediately after opening the ip file descriptor, the udp code uses the up_ipfd field to configure the newly opened ip file descriptor.

int up_ipdev:

up_ipdev is equal to the port number of both the udp physical port and its associated ip physical port (which will be the same). For example, for the configuration file above, the udp physical port (and the associated ip physical port) corresponding to the first line (the ethernet entry) will have its up_ipdev field equal to 0 and the second line (the psip entry) will have its up_ipdev field equal to 1.

acc_t *up_wr_pack:

After a packet (e.g., a udp packet) has its udp header assembled but before it is passed to the lower layer (i.e., the ip layer), the packet is placed in this field. Note that this field is not a queue and that there will be at most a single packet placed here at any given time.


ipaddr_t up_ipaddr:

During the initialization of the udp port, udp_put_data() is called (indirectly) to set the ip address of the udp port to the ip address of the udp port's underlying ip port (which was set either by RARP or the "ifconfig -h host-IP-address" command).


struct udp_fd *up_next_fd:
struct udp_fd *up_write_fd:

If a write operation is suspended, up_write_fd is the udp file descriptor whose write operation is suspended.

struct udp_fd *up_port_any:
struct udp_fd *up_port_hash[UDP_PORT_HASH_NR]:


up_port_any and up_port_hash are both link lists of udp file descriptors. When a udp packet arrives at the ethernet/psip port, udp_ip_arrived() searches through both of these linked lists for the corresponding udp file descriptor(s).

UDP_PORT_HASH_NR is #define'd in udp.c as 16. up_port_hash[] is an array that uses a hash to find the corresponding udp file descriptor quickly.