XINU
rdscomm.c
Go to the documentation of this file.
1 /* rdscomm.c - rdscomm */
2 
3 #include <xinu.h>
4 
5 /*------------------------------------------------------------------------
6  * rdscomm - handle communication with a remote disk server (send a
7  * request and receive a reply, including sequencing and
8  * retries)
9  *------------------------------------------------------------------------
10  */
12  struct rd_msg_hdr *msg, /* Message to send */
13  int32 mlen, /* Message length */
14  struct rd_msg_hdr *reply, /* Buffer for reply */
15  int32 rlen, /* Size of reply buffer */
16  struct rdscblk *rdptr /* Ptr to device control block */
17  )
18 {
19  intmask mask; /* Saved interrupt mask */
20  int32 i; /* Counts retries */
21  int32 retval; /* Return value */
22  int32 seq; /* Sequence for this exchange */
23  uint32 localip; /* Local IP address */
24  int16 rtype; /* Reply type in host byte order*/
25  bool8 xmit; /* Should we transmit again? */
26  int32 slot; /* UDP slot */
27 
28  /* Disable interrupts while testing status */
29 
30  mask = disable();
31 
32  /* Register the server port, if not registered */
33 
34  if ( ! rdptr->rd_registered ) {
35  slot = udp_register(0, rdptr->rd_ser_port,
36  rdptr->rd_loc_port);
37  if(slot == SYSERR) {
38  restore(mask);
39  return SYSERR;
40  }
41  rdptr->rd_udpslot = slot;
42  rdptr->rd_registered = TRUE;
43  }
44 
45  /* Get the local IP address */
46 
47  if ( NetData.ipvalid == FALSE ) {
48  localip = getlocalip();
49  if((int32)localip == SYSERR) {
50  restore(mask);
51  return SYSERR;
52  }
53  }
54  restore(mask);
55 
56  /* Retrieve the saved UDP slot number */
57 
58  slot = rdptr->rd_udpslot;
59 
60  /* Assign message next sequence number */
61 
62  seq = rdptr->rd_seq++;
63  msg->rd_seq = htonl(seq);
64 
65  /* Repeat RD_RETRIES times: send message and receive reply */
66 
67  xmit = TRUE;
68  for (i=0; i<RD_RETRIES; i++) {
69  if (xmit) {
70 
71  /* Send a copy of the message */
72 
73  retval = udp_sendto(slot, rdptr->rd_ser_ip, rdptr->rd_ser_port,
74  (char *)msg, mlen);
75  if (retval == SYSERR) {
76  kprintf("Cannot send to remote disk server\n\r");
77  return SYSERR;
78  }
79  } else {
80  xmit = TRUE;
81  }
82 
83  /* Receive a reply */
84 
85  retval = udp_recv(slot, (char *)reply, rlen,
86  RD_TIMEOUT);
87 
88  if (retval == TIMEOUT) {
89  continue;
90  } else if (retval == SYSERR) {
91  kprintf("Error reading remote disk reply\n\r");
92  return SYSERR;
93  }
94 
95  /* Verify that sequence in reply matches request */
96 
97 
98  if (ntohl(reply->rd_seq) < seq) {
99  xmit = FALSE;
100  } else if (ntohl(reply->rd_seq) != seq) {
101  continue;
102  }
103 
104  /* Verify the type in the reply matches the request */
105 
106  rtype = ntohs(reply->rd_type);
107  if (rtype != ( ntohs(msg->rd_type) | RD_MSG_RESPONSE) ) {
108  continue;
109  }
110 
111  /* Check the status */
112 
113  if (ntohs(reply->rd_status) != 0) {
114  return SYSERR;
115  }
116 
117  return OK;
118  }
119 
120  /* Retries exhausted without success */
121 
122  kprintf("Timeout on exchange with remote disk server\n\r");
123  return TIMEOUT;
124 }
syscall kprintf(char *fmt,...)
ポーリングI/Oを使用して、フォーマットされた文字列をコンソールに出力する。
Definition: kprintf.c:98
int32 udp_recv(uid32, char *, int32, uint32)
Definition: udp.c:146
int32 rd_udpslot
Definition: rdisksys.h:97
uint16 rd_loc_port
Definition: rdisksys.h:95
uint32 rd_ser_ip
Definition: rdisksys.h:93
void restore(intmask)
struct network NetData
Definition: net.c:6
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
bool8 rd_registered
Definition: rdisksys.h:96
#define RD_MSG_RESPONSE
Definition: rdisksys.h:118
#define OK
処理が成功した場合
Definition: kernel.h:77
byte bool8
Boolean値
Definition: kernel.h:36
status rdscomm(struct rd_msg_hdr *msg, int32 mlen, struct rd_msg_hdr *reply, int32 rlen, struct rdscblk *rdptr)
Definition: rdscomm.c:11
#define TIMEOUT
システムコールがタイムアウトした場合
Definition: kernel.h:83
int32 status
ステータスを意味する返り値の型(OK/SYSERR)
Definition: kernel.h:57
#define RD_RETRIES
Definition: rdisksys.h:105
#define ntohl(x)
Definition: prototypes.h:623
uint16 rd_ser_port
Definition: rdisksys.h:94
#define FALSE
Boolean False(0)
Definition: kernel.h:63
#define RD_TIMEOUT
Definition: rdisksys.h:106
#define TRUE
Boolean True(1)
Definition: kernel.h:65
uint32 intmask
保存された割り込みマスク
Definition: kernel.h:38
short int16
符号あり16ビット整数(short)
Definition: kernel.h:13
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
uid32 udp_register(uint32, uint16, uint16)
Definition: udp.c:85
#define ntohs(x)
Definition: prototypes.h:622
uint32 getlocalip(void)
Definition: dhcp.c:142
int32 rd_seq
Definition: rdisksys.h:71
status udp_sendto(uid32, uint32, uint16, char *, int32)
Definition: udp.c:417
#define htonl(x)
Definition: prototypes.h:620
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
bool8 ipvalid
Definition: net.h:63
intmask disable(void)
割り込み禁止(intr.Sに定義がある)