Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Staging: dgrp: Refactor the function dgrp_receive() in drrp_net_ops.c

The function dgrp_receive() in dgrp_net_ops.c is too long and can be
refactored. It uses various switch statements and goto labels. I have
removed a label called data and tried to extract a new function out of
it called as handle_data_in_packet().

This helps to make the code more modularize and simple to read and
understand.

Signed-off-by: Rashika Kheria <rashika.kheria@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Rashika Kheria and committed by
Greg Kroah-Hartman
b73db547 c79bfed6

+175 -155
+175 -155
drivers/staging/dgrp/dgrp_net_ops.c
··· 2232 2232 return rtn; 2233 2233 } 2234 2234 2235 + /* 2236 + * Common Packet Handling code 2237 + */ 2238 + 2239 + static void handle_data_in_packet(struct nd_struct *nd, struct ch_struct *ch, 2240 + long dlen, long plen, int n1, u8 *dbuf) 2241 + { 2242 + char *error; 2243 + long n; 2244 + long remain; 2245 + u8 *buf; 2246 + u8 *b; 2247 + 2248 + remain = nd->nd_remain; 2249 + nd->nd_tx_work = 1; 2250 + 2251 + /* 2252 + * Otherwise data should appear only when we are 2253 + * in the CS_READY state. 2254 + */ 2255 + 2256 + if (ch->ch_state < CS_READY) { 2257 + error = "Data received before RWIN established"; 2258 + nd->nd_remain = 0; 2259 + nd->nd_state = NS_SEND_ERROR; 2260 + nd->nd_error = error; 2261 + } 2262 + 2263 + /* 2264 + * Assure that the data received is within the 2265 + * allowable window. 2266 + */ 2267 + 2268 + n = (ch->ch_s_rwin - ch->ch_s_rin) & 0xffff; 2269 + 2270 + if (dlen > n) { 2271 + error = "Receive data overrun"; 2272 + nd->nd_remain = 0; 2273 + nd->nd_state = NS_SEND_ERROR; 2274 + nd->nd_error = error; 2275 + } 2276 + 2277 + /* 2278 + * If we received 3 or less characters, 2279 + * assume it is a human typing, and set RTIME 2280 + * to 10 milliseconds. 2281 + * 2282 + * If we receive 10 or more characters, 2283 + * assume its not a human typing, and set RTIME 2284 + * to 100 milliseconds. 2285 + */ 2286 + 2287 + if (ch->ch_edelay != DGRP_RTIME) { 2288 + if (ch->ch_rtime != ch->ch_edelay) { 2289 + ch->ch_rtime = ch->ch_edelay; 2290 + ch->ch_flag |= CH_PARAM; 2291 + } 2292 + } else if (dlen <= 3) { 2293 + if (ch->ch_rtime != 10) { 2294 + ch->ch_rtime = 10; 2295 + ch->ch_flag |= CH_PARAM; 2296 + } 2297 + } else { 2298 + if (ch->ch_rtime != DGRP_RTIME) { 2299 + ch->ch_rtime = DGRP_RTIME; 2300 + ch->ch_flag |= CH_PARAM; 2301 + } 2302 + } 2303 + 2304 + /* 2305 + * If a portion of the packet is outside the 2306 + * buffer, shorten the effective length of the 2307 + * data packet to be the amount of data received. 2308 + */ 2309 + 2310 + if (remain < plen) 2311 + dlen -= plen - remain; 2312 + 2313 + /* 2314 + * Detect if receive flush is now complete. 2315 + */ 2316 + 2317 + if ((ch->ch_flag & CH_RX_FLUSH) != 0 && 2318 + ((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >= 2319 + ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) { 2320 + ch->ch_flag &= ~CH_RX_FLUSH; 2321 + } 2322 + 2323 + /* 2324 + * If we are ready to receive, move the data into 2325 + * the receive buffer. 2326 + */ 2327 + 2328 + ch->ch_s_rin = (ch->ch_s_rin + dlen) & 0xffff; 2329 + 2330 + if (ch->ch_state == CS_READY && 2331 + (ch->ch_tun.un_open_count != 0) && 2332 + (ch->ch_tun.un_flag & UN_CLOSING) == 0 && 2333 + (ch->ch_cflag & CF_CREAD) != 0 && 2334 + (ch->ch_flag & (CH_BAUD0 | CH_RX_FLUSH)) == 0 && 2335 + (ch->ch_send & RR_RX_FLUSH) == 0) { 2336 + 2337 + if (ch->ch_rin + dlen >= RBUF_MAX) { 2338 + n = RBUF_MAX - ch->ch_rin; 2339 + 2340 + memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, n); 2341 + 2342 + ch->ch_rin = 0; 2343 + dbuf += n; 2344 + dlen -= n; 2345 + } 2346 + 2347 + memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, dlen); 2348 + 2349 + ch->ch_rin += dlen; 2350 + 2351 + 2352 + /* 2353 + * If we are not in fastcook mode, or 2354 + * if there is a fastcook thread 2355 + * waiting for data, send the data to 2356 + * the line discipline. 2357 + */ 2358 + 2359 + if ((ch->ch_flag & CH_FAST_READ) == 0 || 2360 + ch->ch_inwait != 0) { 2361 + dgrp_input(ch); 2362 + } 2363 + 2364 + /* 2365 + * If there is a read thread waiting 2366 + * in select, and we are in fastcook 2367 + * mode, wake him up. 2368 + */ 2369 + 2370 + if (waitqueue_active(&ch->ch_tun.un_tty->read_wait) && 2371 + (ch->ch_flag & CH_FAST_READ) != 0) 2372 + wake_up_interruptible(&ch->ch_tun.un_tty->read_wait); 2373 + 2374 + /* 2375 + * Wake any thread waiting in the 2376 + * fastcook loop. 2377 + */ 2378 + 2379 + if ((ch->ch_flag & CH_INPUT) != 0) { 2380 + ch->ch_flag &= ~CH_INPUT; 2381 + wake_up_interruptible(&ch->ch_flag_wait); 2382 + } 2383 + } 2384 + 2385 + /* 2386 + * Fabricate and insert a data packet header to 2387 + * preced the remaining data when it comes in. 2388 + */ 2389 + 2390 + if (remain < plen) { 2391 + dlen = plen - remain; 2392 + b = buf; 2393 + 2394 + b[0] = 0x90 + n1; 2395 + put_unaligned_be16(dlen, b + 1); 2396 + 2397 + remain = 3; 2398 + if (remain > 0 && b != buf) 2399 + memcpy(buf, b, remain); 2400 + 2401 + nd->nd_remain = remain; 2402 + return; 2403 + } 2404 + } 2405 + 2235 2406 /** 2236 2407 * dgrp_receive() -- decode data packets received from the remote PortServer. 2237 2408 * @nd: pointer to a node structure ··· 2477 2306 plen = dlen + 1; 2478 2307 2479 2308 dbuf = b + 1; 2480 - goto data; 2309 + handle_data_in_packet(nd, ch, dlen, plen, n1, dbuf); 2310 + break; 2481 2311 2482 2312 /* 2483 2313 * Process 2-byte header data packet. ··· 2492 2320 plen = dlen + 2; 2493 2321 2494 2322 dbuf = b + 2; 2495 - goto data; 2323 + handle_data_in_packet(nd, ch, dlen, plen, n1, dbuf); 2324 + break; 2496 2325 2497 2326 /* 2498 2327 * Process 3-byte header data packet. ··· 2508 2335 2509 2336 dbuf = b + 3; 2510 2337 2511 - /* 2512 - * Common packet handling code. 2513 - */ 2514 - 2515 - data: 2516 - nd->nd_tx_work = 1; 2517 - 2518 - /* 2519 - * Otherwise data should appear only when we are 2520 - * in the CS_READY state. 2521 - */ 2522 - 2523 - if (ch->ch_state < CS_READY) { 2524 - error = "Data received before RWIN established"; 2525 - goto prot_error; 2526 - } 2527 - 2528 - /* 2529 - * Assure that the data received is within the 2530 - * allowable window. 2531 - */ 2532 - 2533 - n = (ch->ch_s_rwin - ch->ch_s_rin) & 0xffff; 2534 - 2535 - if (dlen > n) { 2536 - error = "Receive data overrun"; 2537 - goto prot_error; 2538 - } 2539 - 2540 - /* 2541 - * If we received 3 or less characters, 2542 - * assume it is a human typing, and set RTIME 2543 - * to 10 milliseconds. 2544 - * 2545 - * If we receive 10 or more characters, 2546 - * assume its not a human typing, and set RTIME 2547 - * to 100 milliseconds. 2548 - */ 2549 - 2550 - if (ch->ch_edelay != DGRP_RTIME) { 2551 - if (ch->ch_rtime != ch->ch_edelay) { 2552 - ch->ch_rtime = ch->ch_edelay; 2553 - ch->ch_flag |= CH_PARAM; 2554 - } 2555 - } else if (dlen <= 3) { 2556 - if (ch->ch_rtime != 10) { 2557 - ch->ch_rtime = 10; 2558 - ch->ch_flag |= CH_PARAM; 2559 - } 2560 - } else { 2561 - if (ch->ch_rtime != DGRP_RTIME) { 2562 - ch->ch_rtime = DGRP_RTIME; 2563 - ch->ch_flag |= CH_PARAM; 2564 - } 2565 - } 2566 - 2567 - /* 2568 - * If a portion of the packet is outside the 2569 - * buffer, shorten the effective length of the 2570 - * data packet to be the amount of data received. 2571 - */ 2572 - 2573 - if (remain < plen) 2574 - dlen -= plen - remain; 2575 - 2576 - /* 2577 - * Detect if receive flush is now complete. 2578 - */ 2579 - 2580 - if ((ch->ch_flag & CH_RX_FLUSH) != 0 && 2581 - ((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >= 2582 - ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) { 2583 - ch->ch_flag &= ~CH_RX_FLUSH; 2584 - } 2585 - 2586 - /* 2587 - * If we are ready to receive, move the data into 2588 - * the receive buffer. 2589 - */ 2590 - 2591 - ch->ch_s_rin = (ch->ch_s_rin + dlen) & 0xffff; 2592 - 2593 - if (ch->ch_state == CS_READY && 2594 - (ch->ch_tun.un_open_count != 0) && 2595 - (ch->ch_tun.un_flag & UN_CLOSING) == 0 && 2596 - (ch->ch_cflag & CF_CREAD) != 0 && 2597 - (ch->ch_flag & (CH_BAUD0 | CH_RX_FLUSH)) == 0 && 2598 - (ch->ch_send & RR_RX_FLUSH) == 0) { 2599 - 2600 - if (ch->ch_rin + dlen >= RBUF_MAX) { 2601 - n = RBUF_MAX - ch->ch_rin; 2602 - 2603 - memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, n); 2604 - 2605 - ch->ch_rin = 0; 2606 - dbuf += n; 2607 - dlen -= n; 2608 - } 2609 - 2610 - memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, dlen); 2611 - 2612 - ch->ch_rin += dlen; 2613 - 2614 - 2615 - /* 2616 - * If we are not in fastcook mode, or 2617 - * if there is a fastcook thread 2618 - * waiting for data, send the data to 2619 - * the line discipline. 2620 - */ 2621 - 2622 - if ((ch->ch_flag & CH_FAST_READ) == 0 || 2623 - ch->ch_inwait != 0) { 2624 - dgrp_input(ch); 2625 - } 2626 - 2627 - /* 2628 - * If there is a read thread waiting 2629 - * in select, and we are in fastcook 2630 - * mode, wake him up. 2631 - */ 2632 - 2633 - if (waitqueue_active(&ch->ch_tun.un_tty->read_wait) && 2634 - (ch->ch_flag & CH_FAST_READ) != 0) 2635 - wake_up_interruptible(&ch->ch_tun.un_tty->read_wait); 2636 - 2637 - /* 2638 - * Wake any thread waiting in the 2639 - * fastcook loop. 2640 - */ 2641 - 2642 - if ((ch->ch_flag & CH_INPUT) != 0) { 2643 - ch->ch_flag &= ~CH_INPUT; 2644 - 2645 - wake_up_interruptible(&ch->ch_flag_wait); 2646 - } 2647 - } 2648 - 2649 - /* 2650 - * Fabricate and insert a data packet header to 2651 - * preced the remaining data when it comes in. 2652 - */ 2653 - 2654 - if (remain < plen) { 2655 - dlen = plen - remain; 2656 - b = buf; 2657 - 2658 - b[0] = 0x90 + n1; 2659 - put_unaligned_be16(dlen, b + 1); 2660 - 2661 - remain = 3; 2662 - goto done; 2663 - } 2664 2338 break; 2665 2339 2666 2340 /*