diff -urN a/tc/Makefile b/tc/Makefile
--- a/tc/Makefile	2000-04-16 19:42:53.000000000 +0200
+++ b/tc/Makefile	2003-10-09 08:08:09.000000000 +0200
@@ -26,7 +26,7 @@
 
 #TCMODULES += q_csz.o
 #TCMODULES += q_hpfq.o
-#TCMODULES += q_hfsc.o
+TCMODULES += q_hfsc.o
 
 TCOBJ += $(TCMODULES)
 
diff -urN a/tc/q_hfsc.c b/tc/q_hfsc.c
--- a/tc/q_hfsc.c	2000-04-16 19:42:54.000000000 +0200
+++ b/tc/q_hfsc.c	2003-11-27 02:21:34.000000000 +0100
@@ -1,12 +1,12 @@
 /*
- * q_hfsc.c		HFSC.
+ * q_hfsc.c	HFSC.
  *
  *		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; either version
  *		2 of the License, or (at your option) any later version.
  *
- * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ * Authors:	Patrick McHardy, <kaber@trash.ner>
  *
  */
 
@@ -19,36 +19,368 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <math.h>
 
 #include "utils.h"
 #include "tc_util.h"
 
-static void explain()
+#define HFSC_DEBUG 0
+
+
+static void
+explain_qdisc(void)
+{
+	fprintf(stderr,
+		"Usage: ... hfsc [ default CLASSID ]\n"
+		"\n"
+		" default: default class for unclassified packets\n"
+	);
+}
+
+static void
+explain_class(void)
+{
+	fprintf(stderr, 
+		"Usage: ... hfsc [ rt SC ] [ ls SC ] [ ul SC ]\n"
+		"\n"
+		"SC := [ [ umax BYTE ] dmax SEC ] rate BPS\n"
+		"\n"
+		" umax : maximum unit of work\n"
+		" dmax : maximum delay\n"
+		" rate : rate\n"
+	);
+}
+
+static void
+explain1(char *arg)
+{
+	fprintf(stderr, "HFSC: Illegal \"%s\"\n", arg);
+}
+
+static void
+hfsc_calc_servicecurve(struct tc_service_curve *sc, unsigned int umax,
+                       unsigned int dmax, unsigned int rate)
+{
+	if (dmax != 0 && ceil(umax * 1000000.0 / dmax) > rate) {
+		/*
+		 * concave curve, slope of first segment is umax/dmax,
+		 * intersection is at dmax
+		 */
+		sc->m1 = ceil(umax * 1000000.0 / dmax); /* in bps */
+		sc->d  = dmax; 
+		sc->m2 = rate; 
+	} else {
+		/*
+		 * convex curve, slope of first segment is 0, intersection 
+		 * is at dmax - umax / rate
+		 */
+		sc->m1 = 0;
+		sc->d  = ceil(dmax - umax * 1000000.0 / rate); /* in usec */
+		sc->m2 = rate;
+	}
+}
+
+static int
+hfsc_get_servicecurve(struct tc_service_curve *sc, int *argcp, char ***argvp)
+{
+	char **argv = *argvp;
+	int argc = *argcp;
+	unsigned int umax = 0, dmax = 0, rate = 0;
+
+	if (matches(*argv, "umax") == 0) {
+		NEXT_ARG();
+		if (get_size(&umax, *argv) < 0) {
+			explain1("umax");
+			return -1;
+		}
+		NEXT_ARG();
+	}
+
+	if (matches(*argv, "dmax") == 0) {
+		NEXT_ARG();
+		if (get_usecs(&dmax, *argv) < 0) {
+			explain1("dmax");
+			return -1;
+		}
+		NEXT_ARG();
+	}
+
+	if (matches(*argv, "rate") == 0) {
+		NEXT_ARG();
+		if (get_rate(&rate, *argv) < 0) {
+			explain1("rate");
+			return -1;
+		}
+	} else
+		return -1;
+
+	if (umax != 0 && dmax == 0) {
+		fprintf(stderr, "HFSC: umax given but dmax is zero\n");
+		return -1;
+	}
+	
+	hfsc_calc_servicecurve(sc, umax, dmax, rate);
+	if (sc->m1 == 0 && sc->m2 == 0) {
+		fprintf(stderr, "HFSC: Service Curve has two zero slopes\n");
+		return -1;
+	}
+
+	*argvp = argv;
+	*argcp = argc;
+	return 0;
+}
+
+static void
+hfsc_print_servicecurve(FILE *f, char *name, struct tc_service_curve *sc)
+{
+	unsigned int umax, dmax, rate;
+	SPRINT_BUF(b1);
+
+	if (sc->m1 != 0) {
+		/* concave curve */
+		umax = sc->m1 * sc->d / 1000000;
+		dmax = sc->d;
+		rate = sc->m2;
+	} else {
+		/* convex */
+		if (sc->d == 0) {
+			umax = 0;
+			dmax = 0;
+		} else {
+			umax = 1492;
+			dmax = sc->d + umax * 1000000 / sc->m2;
+		}
+		rate = sc->m2;
+	}
+
+	fprintf(f, "%s ", name);
+	if (umax)
+		fprintf(f, "umax %s ", sprint_size(umax, b1));
+	if (dmax)
+		fprintf(f, "dmax %s ", sprint_usecs(dmax, b1));
+	fprintf(f, "rate %s ", sprint_rate(rate, b1));
+}
+
+static int
+hfsc_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
 {
-	fprintf(stderr, "Usage: ... hfsc \n");
+	struct tc_hfsc_qopt qopt;
+
+	memset(&qopt, 0, sizeof(qopt));
+
+	while (argc > 0) {
+		if (matches(*argv, "default") == 0) {
+			NEXT_ARG();
+			if (qopt.defcls != 0) {
+				fprintf(stderr, "Double \"default\"\n");
+				return -1;
+			}
+			if (get_u16(&qopt.defcls, *argv, 16) < 0) {
+				explain1("default");
+				return -1;
+			}
+		} else if (matches(*argv, "help") == 0) {
+			explain_qdisc();
+			return -1;
+		} else {
+			fprintf(stderr, "What is \"%s\" ?\n", *argv);
+			explain_qdisc();
+			return -1;
+		}
+		argc--, argv++;
+	}
+
+	addattr_l(n, 1024, TCA_OPTIONS, &qopt, sizeof(qopt));
+	return 0;
 }
 
-static void explain1(char *arg)
+static int
+hfsc_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
-	fprintf(stderr, "Illegal \"%s\"\n", arg);
+	struct tc_hfsc_qopt *qopt;
+
+	if (opt == NULL)
+		return 0;
+	if (RTA_PAYLOAD(opt) < sizeof(*qopt))
+		return -1;
+	qopt = RTA_DATA(opt);
+	
+	if (qopt->defcls != 0)
+		fprintf(f, "default %x ", qopt->defcls);
+
+	return 0;
 }
 
+static int
+hfsc_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
+{
+	struct tc_hfsc_stats *st;
+
+	if (xstats == NULL)
+		return 0;
+	if (RTA_PAYLOAD(xstats) < sizeof(*st))
+		return -1;
+	st = RTA_DATA(xstats);
+
+	fprintf(f, " period %u ", st->period);
+	if (st->work != 0)
+		fprintf(f, "work %llu bytes ", st->work);
+	if (st->rtwork != 0)
+		fprintf(f, "rtwork %llu bytes ", st->rtwork);
+	fprintf(f, "level %u ", st->level);
+	fprintf(f, "\n");
 
-#define usage() return(-1)
+	return 0;
+}
 
-static int hfsc_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
+static int
+hfsc_parse_class_opt(struct qdisc_util *qu, int argc, char **argv,
+                     struct nlmsghdr *n)
 {
-	return -1;
+	struct tc_service_curve rsc, fsc, usc;
+	int rsc_ok, fsc_ok, usc_ok;
+	struct rtattr *tail;
+
+	memset(&rsc, 0, sizeof(rsc));
+	memset(&fsc, 0, sizeof(fsc));
+	memset(&usc, 0, sizeof(usc));
+	rsc_ok = fsc_ok = usc_ok = 0;
+
+	while (argc > 0) {
+		if (matches(*argv, "rt") == 0) {
+			NEXT_ARG();
+			if (hfsc_get_servicecurve(&rsc, &argc, &argv) < 0) {
+				explain1("rt");
+				return -1;
+			}
+			rsc_ok = 1;
+		} else if (matches(*argv, "ls") == 0) {
+			NEXT_ARG();
+			if (hfsc_get_servicecurve(&fsc, &argc, &argv) < 0) {
+				explain1("ls");
+				return -1;
+			}
+			fsc_ok = 1;
+		} else if (matches(*argv, "ul") == 0) {
+			NEXT_ARG();
+			if (hfsc_get_servicecurve(&usc, &argc, &argv) < 0) {
+				explain1("ul");
+				return -1;
+			}
+			usc_ok = 1;
+		} else if (matches(*argv, "help") == 0) {
+			explain_class();
+			return -1;
+		} else {
+			fprintf(stderr, "What is \"%s\" ?\n", *argv);
+			explain_class();
+			return -1;
+		}
+		argc--, argv++;
+	}
+
+	if (!(rsc_ok || fsc_ok || usc_ok)) {
+		fprintf(stderr, "HFSC: no parameters given\n");
+		explain_class();
+		return -1;
+	}
+	if (usc_ok && !fsc_ok) {
+		fprintf(stderr, "HFSC: Upper-limit Service Curve without Link-Share Service Curve\n");
+		explain_class();
+		return -1;
+	}
+
+	tail = (struct rtattr*)(((void*)n) + NLMSG_ALIGN(n->nlmsg_len));
+
+	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+	if (rsc_ok)
+		addattr_l(n, 1024, TCA_HFSC_RSC, &rsc, sizeof(rsc));
+	if (fsc_ok)
+		addattr_l(n, 1024, TCA_HFSC_FSC, &fsc, sizeof(fsc));
+	if (usc_ok)
+		addattr_l(n, 1024, TCA_HFSC_USC, &usc, sizeof(usc));
+
+	tail->rta_len = (((void*)n) + NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail;
+	return 0;
 }
 
-static int hfsc_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
+static void
+hfsc_print_class_state(FILE *f, struct tc_hfsc_class_state *st)
 {
-	return -1;
+#if HFSC_DEBUG
+	int leaf = st->cvtmin || st->cfmin ? 0 : 1;
+
+	fprintf(f, "\n ");
+	if (leaf) {
+		fprintf(f, "e %llu ", st->e);
+		fprintf(f, "d %llu ", st->d);
+	}
+	fprintf(f, "f %llu ", st->f);
+	fprintf(f, "myf %llu ", st->myf);
+	fprintf(f, "myfadj %llu ", st->myfadj);
+	if (!leaf) {
+		fprintf(f, "cfmin %llu ", st->cfmin);
+		fprintf(f, "cvtmin %llu ", st->cvtmin);
+		fprintf(f, "cvtmax %llu ", st->cvtmax);
+	}
+	fprintf(f, "\n ");
+	fprintf(f, "vt %llu ", st->vt);
+	fprintf(f, "vtoff %llu ", st->vtoff);
+	fprintf(f, "vtadj %llu ", st->vtadj);
+	fprintf(f, "vtperiod %u ", st->vtperiod);
+	fprintf(f, "parentperiod %u ", st->parentperiod);
+	fprintf(f, "nactive %u ", st->nactive);
+#endif
 }
 
-static int hfsc_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
+static int
+hfsc_print_class_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
-	return -1;
+	struct rtattr *tb[TCA_HFSC_MAX+1];
+	struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
+	struct tc_hfsc_class_state *cstate = NULL;
+	
+	if (opt == NULL)
+		return 0;
+
+	memset(tb, 0, sizeof(tb));
+	parse_rtattr(tb, TCA_HFSC_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
+
+	if (tb[TCA_HFSC_RSC]) {
+		if (RTA_PAYLOAD(tb[TCA_HFSC_RSC]) < sizeof(*rsc))
+			fprintf(stderr, "HFSC: too short realtime option\n");
+		else
+			rsc = RTA_DATA(tb[TCA_HFSC_RSC]);
+	}
+	if (tb[TCA_HFSC_FSC]) {
+		if (RTA_PAYLOAD(tb[TCA_HFSC_FSC]) < sizeof(*fsc))
+			fprintf(stderr, "HFSC: too short linkshare option\n");
+		else
+			fsc = RTA_DATA(tb[TCA_HFSC_FSC]);
+	}
+	if (tb[TCA_HFSC_USC]) {
+		if (RTA_PAYLOAD(tb[TCA_HFSC_USC]) < sizeof(*usc))
+			fprintf(stderr, "HFSC: too short upperlimit option\n");
+		else
+			usc = RTA_DATA(tb[TCA_HFSC_USC]);
+	}
+	if (tb[TCA_HFSC_CSTATE]) {
+		if (RTA_PAYLOAD(tb[TCA_HFSC_CSTATE]) < sizeof(*cstate))
+			fprintf(stderr, "HFSC: too short class state option\n");
+		else
+			cstate = RTA_DATA(tb[TCA_HFSC_CSTATE]);
+	}
+
+	if (rsc != NULL)
+		hfsc_print_servicecurve(f, "ul", rsc);
+	if (fsc != NULL)
+		hfsc_print_servicecurve(f, "ls", fsc);
+	if (usc != NULL) 
+		hfsc_print_servicecurve(f, "ul", usc);
+	if (cstate != NULL)
+		hfsc_print_class_state(f, cstate);
+
+	return 0;
 }
 
 struct qdisc_util hfsc_util = {
@@ -57,5 +389,7 @@
 	hfsc_parse_opt,
 	hfsc_print_opt,
 	hfsc_print_xstats,
+	hfsc_parse_class_opt,
+	hfsc_print_class_opt,
 };
 
