changelog shortlog tags branches changeset file revisions annotate raw help

Mercurial > hg > plan9front / sys/src/9/ip/ipifc.c

revision 7231: 7c0cb11e474d
parent 7230: 3a5ccd543798
child 7233: 24d21d7c0868
     1.1--- a/sys/src/9/ip/ipifc.c
     1.2+++ b/sys/src/9/ip/ipifc.c
     1.3@@ -65,6 +65,9 @@ static void	ipifcregisteraddr(Fs*, Ipifc
     1.4 static void	ipifcregisterproxy(Fs*, Ipifc*, uchar*, int);
     1.5 static char*	ipifcremlifc(Ipifc*, Iplifc**);
     1.6 
     1.7+static char Ebound[] = "interface already bound";
     1.8+static char Eunbound[] = "interface not bound";
     1.9+
    1.10 enum {
    1.11 	unknownv6,		/* UGH */
    1.12 	unspecifiedv6,
    1.13@@ -151,7 +154,7 @@ ipifcbind(Conv *c, char **argv, int argc
    1.14 	wlock(ifc);
    1.15 	if(ifc->m != nil){
    1.16 		wunlock(ifc);
    1.17-		return "interface already bound";
    1.18+		return Ebound;
    1.19 	}
    1.20 	if(waserror()){
    1.21 		wunlock(ifc);
    1.22@@ -172,6 +175,8 @@ ipifcbind(Conv *c, char **argv, int argc
    1.23 	ifc->m = m;
    1.24 	ifc->mintu = ifc->m->mintu;
    1.25 	ifc->maxtu = ifc->m->maxtu;
    1.26+	ifc->delay = 40;
    1.27+	ifc->speed = 0;
    1.28 	if(ifc->m->unbindonclose == 0)
    1.29 		ifc->conv->inuse++;
    1.30 
    1.31@@ -201,7 +206,7 @@ ipifcunbind(Ipifc *ifc)
    1.32 	wlock(ifc);
    1.33 	if(ifc->m == nil){
    1.34 		wunlock(ifc);
    1.35-		return "interface not bound";
    1.36+		return Eunbound;
    1.37 	}
    1.38 
    1.39 	/* disassociate logical interfaces (before zeroing ifc->arg) */
    1.40@@ -300,6 +305,17 @@ ipifcinuse(Conv *c)
    1.41 }
    1.42 
    1.43 static void
    1.44+ipifcadjustburst(Ipifc *ifc)
    1.45+{
    1.46+	int burst;
    1.47+
    1.48+	burst = ((vlong)ifc->delay * ifc->speed) / 8000;
    1.49+	if(burst < ifc->maxtu)
    1.50+		burst = ifc->maxtu;
    1.51+	ifc->burst = burst;
    1.52+}
    1.53+
    1.54+static void
    1.55 ipifcsetdelay(Ipifc *ifc, int delay)
    1.56 {
    1.57 	if(delay < 0)
    1.58@@ -307,9 +323,7 @@ ipifcsetdelay(Ipifc *ifc, int delay)
    1.59 	else if(delay > 1000)
    1.60 		delay = 1000;
    1.61 	ifc->delay = delay;
    1.62-	ifc->burst = ((vlong)delay * ifc->speed) / 8000;
    1.63-	if(ifc->burst < ifc->maxtu)
    1.64-		ifc->burst = ifc->maxtu;
    1.65+	ipifcadjustburst(ifc);
    1.66 }
    1.67 
    1.68 static void
    1.69@@ -319,7 +333,7 @@ ipifcsetspeed(Ipifc *ifc, int speed)
    1.70 		speed = 0;
    1.71 	ifc->speed = speed;
    1.72 	ifc->load = 0;
    1.73-	ipifcsetdelay(ifc, ifc->delay);
    1.74+	ipifcadjustburst(ifc);
    1.75 }
    1.76 
    1.77 void
    1.78@@ -392,8 +406,6 @@ ipifccreate(Conv *c)
    1.79 	ifc->m = nil;
    1.80 	ifc->reflect = 0;
    1.81 	ifc->reassemble = 0;
    1.82-	ipifcsetspeed(ifc, 0);
    1.83-	ipifcsetdelay(ifc, 40);
    1.84 }
    1.85 
    1.86 /*
    1.87@@ -402,11 +414,9 @@ ipifccreate(Conv *c)
    1.88 static void
    1.89 ipifcclose(Conv *c)
    1.90 {
    1.91-	Ipifc *ifc;
    1.92-	Medium *m;
    1.93+	Ipifc *ifc = (Ipifc*)c->ptcl;
    1.94+	Medium *m = ifc->m;
    1.95 
    1.96-	ifc = (Ipifc*)c->ptcl;
    1.97-	m = ifc->m;
    1.98 	if(m != nil && m->unbindonclose)
    1.99 		ipifcunbind(ifc);
   1.100 }
   1.101@@ -414,17 +424,17 @@ ipifcclose(Conv *c)
   1.102 /*
   1.103  *  change an interface's mtu
   1.104  */
   1.105-char*
   1.106-ipifcsetmtu(Ipifc *ifc, char **argv, int argc)
   1.107+static char*
   1.108+ipifcsetmtu(Ipifc *ifc, int mtu)
   1.109 {
   1.110-	int mtu;
   1.111+	Medium *m = ifc->m;
   1.112 
   1.113-	if(argc < 2 || ifc->m == nil)
   1.114-		return Ebadarg;
   1.115-	mtu = strtoul(argv[1], 0, 0);
   1.116-	if(mtu < ifc->m->mintu || mtu > ifc->m->maxtu)
   1.117+	if(m == nil)
   1.118+		return Eunbound;
   1.119+	if(mtu < m->mintu || mtu > m->maxtu)
   1.120 		return Ebadarg;
   1.121 	ifc->maxtu = mtu;
   1.122+	ipifcadjustburst(ifc);
   1.123 	return nil;
   1.124 }
   1.125 
   1.126@@ -488,7 +498,7 @@ ipifcadd(Ipifc *ifc, char **argv, int ar
   1.127 	wlock(ifc);
   1.128 	if(ifc->m == nil){
   1.129 		wunlock(ifc);
   1.130-		return "interface not yet bound to device";
   1.131+		return Eunbound;
   1.132 	}
   1.133 	f = ifc->conv->p->f;
   1.134 	if(waserror()){
   1.135@@ -496,8 +506,8 @@ ipifcadd(Ipifc *ifc, char **argv, int ar
   1.136 		return up->errstr;
   1.137 	}
   1.138 
   1.139-	if(mtu > 0 && mtu >= ifc->m->mintu && mtu <= ifc->m->maxtu)
   1.140-		ifc->maxtu = mtu;
   1.141+	if(mtu > 0)
   1.142+		ipifcsetmtu(ifc, mtu);
   1.143 
   1.144 	/* ignore if this is already a local address for this ifc */
   1.145 	if((lifc = iplocalonifc(ifc, ip)) != nil){
   1.146@@ -680,9 +690,9 @@ done:
   1.147 char*
   1.148 ipifcrem(Ipifc *ifc, char **argv, int argc)
   1.149 {
   1.150-	char *rv;
   1.151 	uchar ip[IPaddrlen], mask[IPaddrlen], rem[IPaddrlen];
   1.152 	Iplifc *lifc, **l;
   1.153+	char *err;
   1.154 
   1.155 	if(argc < 3)
   1.156 		return Ebadarg;
   1.157@@ -707,9 +717,9 @@ ipifcrem(Ipifc *ifc, char **argv, int ar
   1.158 			break;
   1.159 		l = &lifc->next;
   1.160 	}
   1.161-	rv = ipifcremlifc(ifc, l);
   1.162+	err = ipifcremlifc(ifc, l);
   1.163 	wunlock(ifc);
   1.164-	return rv;
   1.165+	return err;
   1.166 }
   1.167 
   1.168 /*
   1.169@@ -720,10 +730,9 @@ ipifcrem(Ipifc *ifc, char **argv, int ar
   1.170 static char*
   1.171 ipifcconnect(Conv* c, char **argv, int argc)
   1.172 {
   1.173+	Ipifc *ifc = (Ipifc*)c->ptcl;
   1.174 	char *err;
   1.175-	Ipifc *ifc;
   1.176 
   1.177-	ifc = (Ipifc*)c->ptcl;
   1.178 	wlock(ifc);
   1.179 	while(ifc->lifc != nil)
   1.180 		ipifcremlifc(ifc, &ifc->lifc);
   1.181@@ -800,9 +809,8 @@ ipifcra6(Ipifc *ifc, char **argv, int ar
   1.182 static char*
   1.183 ipifcctl(Conv* c, char **argv, int argc)
   1.184 {
   1.185-	Ipifc *ifc;
   1.186+	Ipifc *ifc = (Ipifc*)c->ptcl;
   1.187 
   1.188-	ifc = (Ipifc*)c->ptcl;
   1.189 	if(strcmp(argv[0], "add") == 0)
   1.190 		return ipifcadd(ifc, argv, argc, 0, nil);
   1.191 	else if(strcmp(argv[0], "try") == 0)
   1.192@@ -812,7 +820,7 @@ ipifcctl(Conv* c, char **argv, int argc)
   1.193 	else if(strcmp(argv[0], "unbind") == 0)
   1.194 		return ipifcunbind(ifc);
   1.195 	else if(strcmp(argv[0], "mtu") == 0)
   1.196-		return ipifcsetmtu(ifc, argv, argc);
   1.197+		return ipifcsetmtu(ifc, argc>1? strtoul(argv[1], 0, 0): 0);
   1.198 	else if(strcmp(argv[0], "speed") == 0){
   1.199 		ipifcsetspeed(ifc, argc>1? atoi(argv[1]): 0);
   1.200 		return nil;
   1.201@@ -1619,7 +1627,7 @@ ipifcadd6(Ipifc *ifc, char **argv, int a
   1.202 	/* issue "add" ctl msg for v6 link-local addr and prefix len */
   1.203 	m = ifc->m;
   1.204 	if(m == nil || m->pref2addr == nil)
   1.205-		return Ebadarg;
   1.206+		return Eunbound;
   1.207 	(*m->pref2addr)(prefix, ifc->mac);	/* mac → v6 link-local addr */
   1.208 
   1.209 	sprint(addr, "%I", prefix);