datapath.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. unsigned int ovs_net_id __read_mostly;
  58. static struct genl_family dp_packet_genl_family;
  59. static struct genl_family dp_flow_genl_family;
  60. static struct genl_family dp_datapath_genl_family;
  61. static const struct nla_policy flow_policy[];
  62. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  63. .name = OVS_FLOW_MCGROUP,
  64. };
  65. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  66. .name = OVS_DATAPATH_MCGROUP,
  67. };
  68. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  69. .name = OVS_VPORT_MCGROUP,
  70. };
  71. /* Check if need to build a reply message.
  72. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  73. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  74. unsigned int group)
  75. {
  76. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  77. genl_has_listeners(family, genl_info_net(info), group);
  78. }
  79. static void ovs_notify(struct genl_family *family,
  80. struct sk_buff *skb, struct genl_info *info)
  81. {
  82. genl_notify(family, skb, info, 0, GFP_KERNEL);
  83. }
  84. /**
  85. * DOC: Locking:
  86. *
  87. * All writes e.g. Writes to device state (add/remove datapath, port, set
  88. * operations on vports, etc.), Writes to other state (flow table
  89. * modifications, set miscellaneous datapath parameters, etc.) are protected
  90. * by ovs_lock.
  91. *
  92. * Reads are protected by RCU.
  93. *
  94. * There are a few special cases (mostly stats) that have their own
  95. * synchronization but they nest under all of above and don't interact with
  96. * each other.
  97. *
  98. * The RTNL lock nests inside ovs_mutex.
  99. */
  100. static DEFINE_MUTEX(ovs_mutex);
  101. void ovs_lock(void)
  102. {
  103. mutex_lock(&ovs_mutex);
  104. }
  105. void ovs_unlock(void)
  106. {
  107. mutex_unlock(&ovs_mutex);
  108. }
  109. #ifdef CONFIG_LOCKDEP
  110. int lockdep_ovsl_is_held(void)
  111. {
  112. if (debug_locks)
  113. return lockdep_is_held(&ovs_mutex);
  114. else
  115. return 1;
  116. }
  117. #endif
  118. static struct vport *new_vport(const struct vport_parms *);
  119. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  120. const struct sw_flow_key *,
  121. const struct dp_upcall_info *,
  122. uint32_t cutlen);
  123. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  124. const struct sw_flow_key *,
  125. const struct dp_upcall_info *,
  126. uint32_t cutlen);
  127. /* Must be called with rcu_read_lock. */
  128. static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  129. {
  130. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  131. if (dev) {
  132. struct vport *vport = ovs_internal_dev_get_vport(dev);
  133. if (vport)
  134. return vport->dp;
  135. }
  136. return NULL;
  137. }
  138. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  139. * returned dp pointer valid.
  140. */
  141. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  142. {
  143. struct datapath *dp;
  144. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  145. rcu_read_lock();
  146. dp = get_dp_rcu(net, dp_ifindex);
  147. rcu_read_unlock();
  148. return dp;
  149. }
  150. /* Must be called with rcu_read_lock or ovs_mutex. */
  151. const char *ovs_dp_name(const struct datapath *dp)
  152. {
  153. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  154. return ovs_vport_name(vport);
  155. }
  156. static int get_dpifindex(const struct datapath *dp)
  157. {
  158. struct vport *local;
  159. int ifindex;
  160. rcu_read_lock();
  161. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  162. if (local)
  163. ifindex = local->dev->ifindex;
  164. else
  165. ifindex = 0;
  166. rcu_read_unlock();
  167. return ifindex;
  168. }
  169. static void destroy_dp_rcu(struct rcu_head *rcu)
  170. {
  171. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  172. ovs_flow_tbl_destroy(&dp->table);
  173. free_percpu(dp->stats_percpu);
  174. kfree(dp->ports);
  175. kfree(dp);
  176. }
  177. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  178. u16 port_no)
  179. {
  180. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  181. }
  182. /* Called with ovs_mutex or RCU read lock. */
  183. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  184. {
  185. struct vport *vport;
  186. struct hlist_head *head;
  187. head = vport_hash_bucket(dp, port_no);
  188. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  189. if (vport->port_no == port_no)
  190. return vport;
  191. }
  192. return NULL;
  193. }
  194. /* Called with ovs_mutex. */
  195. static struct vport *new_vport(const struct vport_parms *parms)
  196. {
  197. struct vport *vport;
  198. vport = ovs_vport_add(parms);
  199. if (!IS_ERR(vport)) {
  200. struct datapath *dp = parms->dp;
  201. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  202. hlist_add_head_rcu(&vport->dp_hash_node, head);
  203. }
  204. return vport;
  205. }
  206. void ovs_dp_detach_port(struct vport *p)
  207. {
  208. ASSERT_OVSL();
  209. /* First drop references to device. */
  210. hlist_del_rcu(&p->dp_hash_node);
  211. /* Then destroy it. */
  212. ovs_vport_del(p);
  213. }
  214. /* Must be called with rcu_read_lock. */
  215. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  216. {
  217. const struct vport *p = OVS_CB(skb)->input_vport;
  218. struct datapath *dp = p->dp;
  219. struct sw_flow *flow;
  220. struct sw_flow_actions *sf_acts;
  221. struct dp_stats_percpu *stats;
  222. u64 *stats_counter;
  223. u32 n_mask_hit;
  224. stats = this_cpu_ptr(dp->stats_percpu);
  225. /* Look up flow. */
  226. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  227. if (unlikely(!flow)) {
  228. struct dp_upcall_info upcall;
  229. int error;
  230. memset(&upcall, 0, sizeof(upcall));
  231. upcall.cmd = OVS_PACKET_CMD_MISS;
  232. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  233. upcall.mru = OVS_CB(skb)->mru;
  234. error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
  235. if (unlikely(error))
  236. kfree_skb(skb);
  237. else
  238. consume_skb(skb);
  239. stats_counter = &stats->n_missed;
  240. goto out;
  241. }
  242. ovs_flow_stats_update(flow, key->tp.flags, skb);
  243. sf_acts = rcu_dereference(flow->sf_acts);
  244. ovs_execute_actions(dp, skb, sf_acts, key);
  245. stats_counter = &stats->n_hit;
  246. out:
  247. /* Update datapath statistics. */
  248. u64_stats_update_begin(&stats->syncp);
  249. (*stats_counter)++;
  250. stats->n_mask_hit += n_mask_hit;
  251. u64_stats_update_end(&stats->syncp);
  252. }
  253. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  254. const struct sw_flow_key *key,
  255. const struct dp_upcall_info *upcall_info,
  256. uint32_t cutlen)
  257. {
  258. struct dp_stats_percpu *stats;
  259. int err;
  260. if (upcall_info->portid == 0) {
  261. err = -ENOTCONN;
  262. goto err;
  263. }
  264. if (!skb_is_gso(skb))
  265. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  266. else
  267. err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
  268. if (err)
  269. goto err;
  270. return 0;
  271. err:
  272. stats = this_cpu_ptr(dp->stats_percpu);
  273. u64_stats_update_begin(&stats->syncp);
  274. stats->n_lost++;
  275. u64_stats_update_end(&stats->syncp);
  276. return err;
  277. }
  278. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  279. const struct sw_flow_key *key,
  280. const struct dp_upcall_info *upcall_info,
  281. uint32_t cutlen)
  282. {
  283. unsigned int gso_type = skb_shinfo(skb)->gso_type;
  284. struct sw_flow_key later_key;
  285. struct sk_buff *segs, *nskb;
  286. int err;
  287. BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
  288. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  289. if (IS_ERR(segs))
  290. return PTR_ERR(segs);
  291. if (segs == NULL)
  292. return -EINVAL;
  293. if (gso_type & SKB_GSO_UDP) {
  294. /* The initial flow key extracted by ovs_flow_key_extract()
  295. * in this case is for a first fragment, so we need to
  296. * properly mark later fragments.
  297. */
  298. later_key = *key;
  299. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  300. }
  301. /* Queue all of the segments. */
  302. skb = segs;
  303. do {
  304. if (gso_type & SKB_GSO_UDP && skb != segs)
  305. key = &later_key;
  306. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  307. if (err)
  308. break;
  309. } while ((skb = skb->next));
  310. /* Free all of the segments. */
  311. skb = segs;
  312. do {
  313. nskb = skb->next;
  314. if (err)
  315. kfree_skb(skb);
  316. else
  317. consume_skb(skb);
  318. } while ((skb = nskb));
  319. return err;
  320. }
  321. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  322. unsigned int hdrlen, int actions_attrlen)
  323. {
  324. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  325. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  326. + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
  327. + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
  328. /* OVS_PACKET_ATTR_USERDATA */
  329. if (upcall_info->userdata)
  330. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  331. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  332. if (upcall_info->egress_tun_info)
  333. size += nla_total_size(ovs_tun_key_attr_size());
  334. /* OVS_PACKET_ATTR_ACTIONS */
  335. if (upcall_info->actions_len)
  336. size += nla_total_size(actions_attrlen);
  337. /* OVS_PACKET_ATTR_MRU */
  338. if (upcall_info->mru)
  339. size += nla_total_size(sizeof(upcall_info->mru));
  340. return size;
  341. }
  342. static void pad_packet(struct datapath *dp, struct sk_buff *skb)
  343. {
  344. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  345. size_t plen = NLA_ALIGN(skb->len) - skb->len;
  346. if (plen > 0)
  347. skb_put_zero(skb, plen);
  348. }
  349. }
  350. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  351. const struct sw_flow_key *key,
  352. const struct dp_upcall_info *upcall_info,
  353. uint32_t cutlen)
  354. {
  355. struct ovs_header *upcall;
  356. struct sk_buff *nskb = NULL;
  357. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  358. struct nlattr *nla;
  359. size_t len;
  360. unsigned int hlen;
  361. int err, dp_ifindex;
  362. dp_ifindex = get_dpifindex(dp);
  363. if (!dp_ifindex)
  364. return -ENODEV;
  365. if (skb_vlan_tag_present(skb)) {
  366. nskb = skb_clone(skb, GFP_ATOMIC);
  367. if (!nskb)
  368. return -ENOMEM;
  369. nskb = __vlan_hwaccel_push_inside(nskb);
  370. if (!nskb)
  371. return -ENOMEM;
  372. skb = nskb;
  373. }
  374. if (nla_attr_size(skb->len) > USHRT_MAX) {
  375. err = -EFBIG;
  376. goto out;
  377. }
  378. /* Complete checksum if needed */
  379. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  380. (err = skb_csum_hwoffload_help(skb, 0)))
  381. goto out;
  382. /* Older versions of OVS user space enforce alignment of the last
  383. * Netlink attribute to NLA_ALIGNTO which would require extensive
  384. * padding logic. Only perform zerocopy if padding is not required.
  385. */
  386. if (dp->user_features & OVS_DP_F_UNALIGNED)
  387. hlen = skb_zerocopy_headlen(skb);
  388. else
  389. hlen = skb->len;
  390. len = upcall_msg_size(upcall_info, hlen - cutlen,
  391. OVS_CB(skb)->acts_origlen);
  392. user_skb = genlmsg_new(len, GFP_ATOMIC);
  393. if (!user_skb) {
  394. err = -ENOMEM;
  395. goto out;
  396. }
  397. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  398. 0, upcall_info->cmd);
  399. upcall->dp_ifindex = dp_ifindex;
  400. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  401. BUG_ON(err);
  402. if (upcall_info->userdata)
  403. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  404. nla_len(upcall_info->userdata),
  405. nla_data(upcall_info->userdata));
  406. if (upcall_info->egress_tun_info) {
  407. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  408. err = ovs_nla_put_tunnel_info(user_skb,
  409. upcall_info->egress_tun_info);
  410. BUG_ON(err);
  411. nla_nest_end(user_skb, nla);
  412. }
  413. if (upcall_info->actions_len) {
  414. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
  415. err = ovs_nla_put_actions(upcall_info->actions,
  416. upcall_info->actions_len,
  417. user_skb);
  418. if (!err)
  419. nla_nest_end(user_skb, nla);
  420. else
  421. nla_nest_cancel(user_skb, nla);
  422. }
  423. /* Add OVS_PACKET_ATTR_MRU */
  424. if (upcall_info->mru) {
  425. if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
  426. upcall_info->mru)) {
  427. err = -ENOBUFS;
  428. goto out;
  429. }
  430. pad_packet(dp, user_skb);
  431. }
  432. /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
  433. if (cutlen > 0) {
  434. if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
  435. skb->len)) {
  436. err = -ENOBUFS;
  437. goto out;
  438. }
  439. pad_packet(dp, user_skb);
  440. }
  441. /* Only reserve room for attribute header, packet data is added
  442. * in skb_zerocopy() */
  443. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  444. err = -ENOBUFS;
  445. goto out;
  446. }
  447. nla->nla_len = nla_attr_size(skb->len - cutlen);
  448. err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
  449. if (err)
  450. goto out;
  451. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  452. pad_packet(dp, user_skb);
  453. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  454. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  455. user_skb = NULL;
  456. out:
  457. if (err)
  458. skb_tx_error(skb);
  459. kfree_skb(user_skb);
  460. kfree_skb(nskb);
  461. return err;
  462. }
  463. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  464. {
  465. struct ovs_header *ovs_header = info->userhdr;
  466. struct net *net = sock_net(skb->sk);
  467. struct nlattr **a = info->attrs;
  468. struct sw_flow_actions *acts;
  469. struct sk_buff *packet;
  470. struct sw_flow *flow;
  471. struct sw_flow_actions *sf_acts;
  472. struct datapath *dp;
  473. struct vport *input_vport;
  474. u16 mru = 0;
  475. int len;
  476. int err;
  477. bool log = !a[OVS_PACKET_ATTR_PROBE];
  478. err = -EINVAL;
  479. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  480. !a[OVS_PACKET_ATTR_ACTIONS])
  481. goto err;
  482. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  483. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  484. err = -ENOMEM;
  485. if (!packet)
  486. goto err;
  487. skb_reserve(packet, NET_IP_ALIGN);
  488. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  489. /* Set packet's mru */
  490. if (a[OVS_PACKET_ATTR_MRU]) {
  491. mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
  492. packet->ignore_df = 1;
  493. }
  494. OVS_CB(packet)->mru = mru;
  495. /* Build an sw_flow for sending this packet. */
  496. flow = ovs_flow_alloc();
  497. err = PTR_ERR(flow);
  498. if (IS_ERR(flow))
  499. goto err_kfree_skb;
  500. err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
  501. packet, &flow->key, log);
  502. if (err)
  503. goto err_flow_free;
  504. err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
  505. &flow->key, &acts, log);
  506. if (err)
  507. goto err_flow_free;
  508. rcu_assign_pointer(flow->sf_acts, acts);
  509. packet->priority = flow->key.phy.priority;
  510. packet->mark = flow->key.phy.skb_mark;
  511. rcu_read_lock();
  512. dp = get_dp_rcu(net, ovs_header->dp_ifindex);
  513. err = -ENODEV;
  514. if (!dp)
  515. goto err_unlock;
  516. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  517. if (!input_vport)
  518. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  519. if (!input_vport)
  520. goto err_unlock;
  521. packet->dev = input_vport->dev;
  522. OVS_CB(packet)->input_vport = input_vport;
  523. sf_acts = rcu_dereference(flow->sf_acts);
  524. local_bh_disable();
  525. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  526. local_bh_enable();
  527. rcu_read_unlock();
  528. ovs_flow_free(flow, false);
  529. return err;
  530. err_unlock:
  531. rcu_read_unlock();
  532. err_flow_free:
  533. ovs_flow_free(flow, false);
  534. err_kfree_skb:
  535. kfree_skb(packet);
  536. err:
  537. return err;
  538. }
  539. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  540. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  541. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  542. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  543. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  544. [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
  545. };
  546. static const struct genl_ops dp_packet_genl_ops[] = {
  547. { .cmd = OVS_PACKET_CMD_EXECUTE,
  548. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  549. .policy = packet_policy,
  550. .doit = ovs_packet_cmd_execute
  551. }
  552. };
  553. static struct genl_family dp_packet_genl_family __ro_after_init = {
  554. .hdrsize = sizeof(struct ovs_header),
  555. .name = OVS_PACKET_FAMILY,
  556. .version = OVS_PACKET_VERSION,
  557. .maxattr = OVS_PACKET_ATTR_MAX,
  558. .netnsok = true,
  559. .parallel_ops = true,
  560. .ops = dp_packet_genl_ops,
  561. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  562. .module = THIS_MODULE,
  563. };
  564. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  565. struct ovs_dp_megaflow_stats *mega_stats)
  566. {
  567. int i;
  568. memset(mega_stats, 0, sizeof(*mega_stats));
  569. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  570. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  571. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  572. for_each_possible_cpu(i) {
  573. const struct dp_stats_percpu *percpu_stats;
  574. struct dp_stats_percpu local_stats;
  575. unsigned int start;
  576. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  577. do {
  578. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  579. local_stats = *percpu_stats;
  580. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  581. stats->n_hit += local_stats.n_hit;
  582. stats->n_missed += local_stats.n_missed;
  583. stats->n_lost += local_stats.n_lost;
  584. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  585. }
  586. }
  587. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  588. {
  589. return ovs_identifier_is_ufid(sfid) &&
  590. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  591. }
  592. static bool should_fill_mask(uint32_t ufid_flags)
  593. {
  594. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  595. }
  596. static bool should_fill_actions(uint32_t ufid_flags)
  597. {
  598. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  599. }
  600. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  601. const struct sw_flow_id *sfid,
  602. uint32_t ufid_flags)
  603. {
  604. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  605. /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
  606. * see ovs_nla_put_identifier()
  607. */
  608. if (sfid && ovs_identifier_is_ufid(sfid))
  609. len += nla_total_size(sfid->ufid_len);
  610. else
  611. len += nla_total_size(ovs_key_attr_size());
  612. /* OVS_FLOW_ATTR_KEY */
  613. if (!sfid || should_fill_key(sfid, ufid_flags))
  614. len += nla_total_size(ovs_key_attr_size());
  615. /* OVS_FLOW_ATTR_MASK */
  616. if (should_fill_mask(ufid_flags))
  617. len += nla_total_size(ovs_key_attr_size());
  618. /* OVS_FLOW_ATTR_ACTIONS */
  619. if (should_fill_actions(ufid_flags))
  620. len += nla_total_size(acts->orig_len);
  621. return len
  622. + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  623. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  624. + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
  625. }
  626. /* Called with ovs_mutex or RCU read lock. */
  627. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  628. struct sk_buff *skb)
  629. {
  630. struct ovs_flow_stats stats;
  631. __be16 tcp_flags;
  632. unsigned long used;
  633. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  634. if (used &&
  635. nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
  636. OVS_FLOW_ATTR_PAD))
  637. return -EMSGSIZE;
  638. if (stats.n_packets &&
  639. nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
  640. sizeof(struct ovs_flow_stats), &stats,
  641. OVS_FLOW_ATTR_PAD))
  642. return -EMSGSIZE;
  643. if ((u8)ntohs(tcp_flags) &&
  644. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  645. return -EMSGSIZE;
  646. return 0;
  647. }
  648. /* Called with ovs_mutex or RCU read lock. */
  649. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  650. struct sk_buff *skb, int skb_orig_len)
  651. {
  652. struct nlattr *start;
  653. int err;
  654. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  655. * this is the first flow to be dumped into 'skb'. This is unusual for
  656. * Netlink but individual action lists can be longer than
  657. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  658. * The userspace caller can always fetch the actions separately if it
  659. * really wants them. (Most userspace callers in fact don't care.)
  660. *
  661. * This can only fail for dump operations because the skb is always
  662. * properly sized for single flows.
  663. */
  664. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  665. if (start) {
  666. const struct sw_flow_actions *sf_acts;
  667. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  668. err = ovs_nla_put_actions(sf_acts->actions,
  669. sf_acts->actions_len, skb);
  670. if (!err)
  671. nla_nest_end(skb, start);
  672. else {
  673. if (skb_orig_len)
  674. return err;
  675. nla_nest_cancel(skb, start);
  676. }
  677. } else if (skb_orig_len) {
  678. return -EMSGSIZE;
  679. }
  680. return 0;
  681. }
  682. /* Called with ovs_mutex or RCU read lock. */
  683. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  684. struct sk_buff *skb, u32 portid,
  685. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  686. {
  687. const int skb_orig_len = skb->len;
  688. struct ovs_header *ovs_header;
  689. int err;
  690. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  691. flags, cmd);
  692. if (!ovs_header)
  693. return -EMSGSIZE;
  694. ovs_header->dp_ifindex = dp_ifindex;
  695. err = ovs_nla_put_identifier(flow, skb);
  696. if (err)
  697. goto error;
  698. if (should_fill_key(&flow->id, ufid_flags)) {
  699. err = ovs_nla_put_masked_key(flow, skb);
  700. if (err)
  701. goto error;
  702. }
  703. if (should_fill_mask(ufid_flags)) {
  704. err = ovs_nla_put_mask(flow, skb);
  705. if (err)
  706. goto error;
  707. }
  708. err = ovs_flow_cmd_fill_stats(flow, skb);
  709. if (err)
  710. goto error;
  711. if (should_fill_actions(ufid_flags)) {
  712. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  713. if (err)
  714. goto error;
  715. }
  716. genlmsg_end(skb, ovs_header);
  717. return 0;
  718. error:
  719. genlmsg_cancel(skb, ovs_header);
  720. return err;
  721. }
  722. /* May not be called with RCU read lock. */
  723. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  724. const struct sw_flow_id *sfid,
  725. struct genl_info *info,
  726. bool always,
  727. uint32_t ufid_flags)
  728. {
  729. struct sk_buff *skb;
  730. size_t len;
  731. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  732. return NULL;
  733. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  734. skb = genlmsg_new(len, GFP_KERNEL);
  735. if (!skb)
  736. return ERR_PTR(-ENOMEM);
  737. return skb;
  738. }
  739. /* Called with ovs_mutex. */
  740. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  741. int dp_ifindex,
  742. struct genl_info *info, u8 cmd,
  743. bool always, u32 ufid_flags)
  744. {
  745. struct sk_buff *skb;
  746. int retval;
  747. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  748. &flow->id, info, always, ufid_flags);
  749. if (IS_ERR_OR_NULL(skb))
  750. return skb;
  751. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  752. info->snd_portid, info->snd_seq, 0,
  753. cmd, ufid_flags);
  754. if (WARN_ON_ONCE(retval < 0)) {
  755. kfree_skb(skb);
  756. skb = ERR_PTR(retval);
  757. }
  758. return skb;
  759. }
  760. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  761. {
  762. struct net *net = sock_net(skb->sk);
  763. struct nlattr **a = info->attrs;
  764. struct ovs_header *ovs_header = info->userhdr;
  765. struct sw_flow *flow = NULL, *new_flow;
  766. struct sw_flow_mask mask;
  767. struct sk_buff *reply;
  768. struct datapath *dp;
  769. struct sw_flow_actions *acts;
  770. struct sw_flow_match match;
  771. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  772. int error;
  773. bool log = !a[OVS_FLOW_ATTR_PROBE];
  774. /* Must have key and actions. */
  775. error = -EINVAL;
  776. if (!a[OVS_FLOW_ATTR_KEY]) {
  777. OVS_NLERR(log, "Flow key attr not present in new flow.");
  778. goto error;
  779. }
  780. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  781. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  782. goto error;
  783. }
  784. /* Most of the time we need to allocate a new flow, do it before
  785. * locking.
  786. */
  787. new_flow = ovs_flow_alloc();
  788. if (IS_ERR(new_flow)) {
  789. error = PTR_ERR(new_flow);
  790. goto error;
  791. }
  792. /* Extract key. */
  793. ovs_match_init(&match, &new_flow->key, false, &mask);
  794. error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  795. a[OVS_FLOW_ATTR_MASK], log);
  796. if (error)
  797. goto err_kfree_flow;
  798. /* Extract flow identifier. */
  799. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  800. &new_flow->key, log);
  801. if (error)
  802. goto err_kfree_flow;
  803. /* unmasked key is needed to match when ufid is not used. */
  804. if (ovs_identifier_is_key(&new_flow->id))
  805. match.key = new_flow->id.unmasked_key;
  806. ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
  807. /* Validate actions. */
  808. error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
  809. &new_flow->key, &acts, log);
  810. if (error) {
  811. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  812. goto err_kfree_flow;
  813. }
  814. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  815. ufid_flags);
  816. if (IS_ERR(reply)) {
  817. error = PTR_ERR(reply);
  818. goto err_kfree_acts;
  819. }
  820. ovs_lock();
  821. dp = get_dp(net, ovs_header->dp_ifindex);
  822. if (unlikely(!dp)) {
  823. error = -ENODEV;
  824. goto err_unlock_ovs;
  825. }
  826. /* Check if this is a duplicate flow */
  827. if (ovs_identifier_is_ufid(&new_flow->id))
  828. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  829. if (!flow)
  830. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
  831. if (likely(!flow)) {
  832. rcu_assign_pointer(new_flow->sf_acts, acts);
  833. /* Put flow in bucket. */
  834. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  835. if (unlikely(error)) {
  836. acts = NULL;
  837. goto err_unlock_ovs;
  838. }
  839. if (unlikely(reply)) {
  840. error = ovs_flow_cmd_fill_info(new_flow,
  841. ovs_header->dp_ifindex,
  842. reply, info->snd_portid,
  843. info->snd_seq, 0,
  844. OVS_FLOW_CMD_NEW,
  845. ufid_flags);
  846. BUG_ON(error < 0);
  847. }
  848. ovs_unlock();
  849. } else {
  850. struct sw_flow_actions *old_acts;
  851. /* Bail out if we're not allowed to modify an existing flow.
  852. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  853. * because Generic Netlink treats the latter as a dump
  854. * request. We also accept NLM_F_EXCL in case that bug ever
  855. * gets fixed.
  856. */
  857. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  858. | NLM_F_EXCL))) {
  859. error = -EEXIST;
  860. goto err_unlock_ovs;
  861. }
  862. /* The flow identifier has to be the same for flow updates.
  863. * Look for any overlapping flow.
  864. */
  865. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  866. if (ovs_identifier_is_key(&flow->id))
  867. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  868. &match);
  869. else /* UFID matches but key is different */
  870. flow = NULL;
  871. if (!flow) {
  872. error = -ENOENT;
  873. goto err_unlock_ovs;
  874. }
  875. }
  876. /* Update actions. */
  877. old_acts = ovsl_dereference(flow->sf_acts);
  878. rcu_assign_pointer(flow->sf_acts, acts);
  879. if (unlikely(reply)) {
  880. error = ovs_flow_cmd_fill_info(flow,
  881. ovs_header->dp_ifindex,
  882. reply, info->snd_portid,
  883. info->snd_seq, 0,
  884. OVS_FLOW_CMD_NEW,
  885. ufid_flags);
  886. BUG_ON(error < 0);
  887. }
  888. ovs_unlock();
  889. ovs_nla_free_flow_actions_rcu(old_acts);
  890. ovs_flow_free(new_flow, false);
  891. }
  892. if (reply)
  893. ovs_notify(&dp_flow_genl_family, reply, info);
  894. return 0;
  895. err_unlock_ovs:
  896. ovs_unlock();
  897. kfree_skb(reply);
  898. err_kfree_acts:
  899. ovs_nla_free_flow_actions(acts);
  900. err_kfree_flow:
  901. ovs_flow_free(new_flow, false);
  902. error:
  903. return error;
  904. }
  905. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  906. static struct sw_flow_actions *get_flow_actions(struct net *net,
  907. const struct nlattr *a,
  908. const struct sw_flow_key *key,
  909. const struct sw_flow_mask *mask,
  910. bool log)
  911. {
  912. struct sw_flow_actions *acts;
  913. struct sw_flow_key masked_key;
  914. int error;
  915. ovs_flow_mask_key(&masked_key, key, true, mask);
  916. error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
  917. if (error) {
  918. OVS_NLERR(log,
  919. "Actions may not be safe on all matching packets");
  920. return ERR_PTR(error);
  921. }
  922. return acts;
  923. }
  924. /* Factor out match-init and action-copy to avoid
  925. * "Wframe-larger-than=1024" warning. Because mask is only
  926. * used to get actions, we new a function to save some
  927. * stack space.
  928. *
  929. * If there are not key and action attrs, we return 0
  930. * directly. In the case, the caller will also not use the
  931. * match as before. If there is action attr, we try to get
  932. * actions and save them to *acts. Before returning from
  933. * the function, we reset the match->mask pointer. Because
  934. * we should not to return match object with dangling reference
  935. * to mask.
  936. * */
  937. static int ovs_nla_init_match_and_action(struct net *net,
  938. struct sw_flow_match *match,
  939. struct sw_flow_key *key,
  940. struct nlattr **a,
  941. struct sw_flow_actions **acts,
  942. bool log)
  943. {
  944. struct sw_flow_mask mask;
  945. int error = 0;
  946. if (a[OVS_FLOW_ATTR_KEY]) {
  947. ovs_match_init(match, key, true, &mask);
  948. error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
  949. a[OVS_FLOW_ATTR_MASK], log);
  950. if (error)
  951. goto error;
  952. }
  953. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  954. if (!a[OVS_FLOW_ATTR_KEY]) {
  955. OVS_NLERR(log,
  956. "Flow key attribute not present in set flow.");
  957. error = -EINVAL;
  958. goto error;
  959. }
  960. *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
  961. &mask, log);
  962. if (IS_ERR(*acts)) {
  963. error = PTR_ERR(*acts);
  964. goto error;
  965. }
  966. }
  967. /* On success, error is 0. */
  968. error:
  969. match->mask = NULL;
  970. return error;
  971. }
  972. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  973. {
  974. struct net *net = sock_net(skb->sk);
  975. struct nlattr **a = info->attrs;
  976. struct ovs_header *ovs_header = info->userhdr;
  977. struct sw_flow_key key;
  978. struct sw_flow *flow;
  979. struct sk_buff *reply = NULL;
  980. struct datapath *dp;
  981. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  982. struct sw_flow_match match;
  983. struct sw_flow_id sfid;
  984. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  985. int error = 0;
  986. bool log = !a[OVS_FLOW_ATTR_PROBE];
  987. bool ufid_present;
  988. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  989. if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
  990. OVS_NLERR(log,
  991. "Flow set message rejected, Key attribute missing.");
  992. return -EINVAL;
  993. }
  994. error = ovs_nla_init_match_and_action(net, &match, &key, a,
  995. &acts, log);
  996. if (error)
  997. goto error;
  998. if (acts) {
  999. /* Can allocate before locking if have acts. */
  1000. reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
  1001. ufid_flags);
  1002. if (IS_ERR(reply)) {
  1003. error = PTR_ERR(reply);
  1004. goto err_kfree_acts;
  1005. }
  1006. }
  1007. ovs_lock();
  1008. dp = get_dp(net, ovs_header->dp_ifindex);
  1009. if (unlikely(!dp)) {
  1010. error = -ENODEV;
  1011. goto err_unlock_ovs;
  1012. }
  1013. /* Check that the flow exists. */
  1014. if (ufid_present)
  1015. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
  1016. else
  1017. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1018. if (unlikely(!flow)) {
  1019. error = -ENOENT;
  1020. goto err_unlock_ovs;
  1021. }
  1022. /* Update actions, if present. */
  1023. if (likely(acts)) {
  1024. old_acts = ovsl_dereference(flow->sf_acts);
  1025. rcu_assign_pointer(flow->sf_acts, acts);
  1026. if (unlikely(reply)) {
  1027. error = ovs_flow_cmd_fill_info(flow,
  1028. ovs_header->dp_ifindex,
  1029. reply, info->snd_portid,
  1030. info->snd_seq, 0,
  1031. OVS_FLOW_CMD_NEW,
  1032. ufid_flags);
  1033. BUG_ON(error < 0);
  1034. }
  1035. } else {
  1036. /* Could not alloc without acts before locking. */
  1037. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  1038. info, OVS_FLOW_CMD_NEW, false,
  1039. ufid_flags);
  1040. if (IS_ERR(reply)) {
  1041. error = PTR_ERR(reply);
  1042. goto err_unlock_ovs;
  1043. }
  1044. }
  1045. /* Clear stats. */
  1046. if (a[OVS_FLOW_ATTR_CLEAR])
  1047. ovs_flow_stats_clear(flow);
  1048. ovs_unlock();
  1049. if (reply)
  1050. ovs_notify(&dp_flow_genl_family, reply, info);
  1051. if (old_acts)
  1052. ovs_nla_free_flow_actions_rcu(old_acts);
  1053. return 0;
  1054. err_unlock_ovs:
  1055. ovs_unlock();
  1056. kfree_skb(reply);
  1057. err_kfree_acts:
  1058. ovs_nla_free_flow_actions(acts);
  1059. error:
  1060. return error;
  1061. }
  1062. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1063. {
  1064. struct nlattr **a = info->attrs;
  1065. struct ovs_header *ovs_header = info->userhdr;
  1066. struct net *net = sock_net(skb->sk);
  1067. struct sw_flow_key key;
  1068. struct sk_buff *reply;
  1069. struct sw_flow *flow;
  1070. struct datapath *dp;
  1071. struct sw_flow_match match;
  1072. struct sw_flow_id ufid;
  1073. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1074. int err = 0;
  1075. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1076. bool ufid_present;
  1077. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1078. if (a[OVS_FLOW_ATTR_KEY]) {
  1079. ovs_match_init(&match, &key, true, NULL);
  1080. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
  1081. log);
  1082. } else if (!ufid_present) {
  1083. OVS_NLERR(log,
  1084. "Flow get message rejected, Key attribute missing.");
  1085. err = -EINVAL;
  1086. }
  1087. if (err)
  1088. return err;
  1089. ovs_lock();
  1090. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1091. if (!dp) {
  1092. err = -ENODEV;
  1093. goto unlock;
  1094. }
  1095. if (ufid_present)
  1096. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1097. else
  1098. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1099. if (!flow) {
  1100. err = -ENOENT;
  1101. goto unlock;
  1102. }
  1103. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  1104. OVS_FLOW_CMD_NEW, true, ufid_flags);
  1105. if (IS_ERR(reply)) {
  1106. err = PTR_ERR(reply);
  1107. goto unlock;
  1108. }
  1109. ovs_unlock();
  1110. return genlmsg_reply(reply, info);
  1111. unlock:
  1112. ovs_unlock();
  1113. return err;
  1114. }
  1115. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1116. {
  1117. struct nlattr **a = info->attrs;
  1118. struct ovs_header *ovs_header = info->userhdr;
  1119. struct net *net = sock_net(skb->sk);
  1120. struct sw_flow_key key;
  1121. struct sk_buff *reply;
  1122. struct sw_flow *flow = NULL;
  1123. struct datapath *dp;
  1124. struct sw_flow_match match;
  1125. struct sw_flow_id ufid;
  1126. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1127. int err;
  1128. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1129. bool ufid_present;
  1130. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1131. if (a[OVS_FLOW_ATTR_KEY]) {
  1132. ovs_match_init(&match, &key, true, NULL);
  1133. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  1134. NULL, log);
  1135. if (unlikely(err))
  1136. return err;
  1137. }
  1138. ovs_lock();
  1139. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1140. if (unlikely(!dp)) {
  1141. err = -ENODEV;
  1142. goto unlock;
  1143. }
  1144. if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
  1145. err = ovs_flow_tbl_flush(&dp->table);
  1146. goto unlock;
  1147. }
  1148. if (ufid_present)
  1149. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1150. else
  1151. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1152. if (unlikely(!flow)) {
  1153. err = -ENOENT;
  1154. goto unlock;
  1155. }
  1156. ovs_flow_tbl_remove(&dp->table, flow);
  1157. ovs_unlock();
  1158. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1159. &flow->id, info, false, ufid_flags);
  1160. if (likely(reply)) {
  1161. if (likely(!IS_ERR(reply))) {
  1162. rcu_read_lock(); /*To keep RCU checker happy. */
  1163. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1164. reply, info->snd_portid,
  1165. info->snd_seq, 0,
  1166. OVS_FLOW_CMD_DEL,
  1167. ufid_flags);
  1168. rcu_read_unlock();
  1169. if (WARN_ON_ONCE(err < 0)) {
  1170. kfree_skb(reply);
  1171. goto out_free;
  1172. }
  1173. ovs_notify(&dp_flow_genl_family, reply, info);
  1174. } else {
  1175. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1176. }
  1177. }
  1178. out_free:
  1179. ovs_flow_free(flow, true);
  1180. return 0;
  1181. unlock:
  1182. ovs_unlock();
  1183. return err;
  1184. }
  1185. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1186. {
  1187. struct nlattr *a[__OVS_FLOW_ATTR_MAX];
  1188. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1189. struct table_instance *ti;
  1190. struct datapath *dp;
  1191. u32 ufid_flags;
  1192. int err;
  1193. err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
  1194. OVS_FLOW_ATTR_MAX, flow_policy, NULL);
  1195. if (err)
  1196. return err;
  1197. ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1198. rcu_read_lock();
  1199. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1200. if (!dp) {
  1201. rcu_read_unlock();
  1202. return -ENODEV;
  1203. }
  1204. ti = rcu_dereference(dp->table.ti);
  1205. for (;;) {
  1206. struct sw_flow *flow;
  1207. u32 bucket, obj;
  1208. bucket = cb->args[0];
  1209. obj = cb->args[1];
  1210. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1211. if (!flow)
  1212. break;
  1213. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1214. NETLINK_CB(cb->skb).portid,
  1215. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1216. OVS_FLOW_CMD_NEW, ufid_flags) < 0)
  1217. break;
  1218. cb->args[0] = bucket;
  1219. cb->args[1] = obj;
  1220. }
  1221. rcu_read_unlock();
  1222. return skb->len;
  1223. }
  1224. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1225. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1226. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1227. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1228. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1229. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1230. [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
  1231. [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
  1232. };
  1233. static const struct genl_ops dp_flow_genl_ops[] = {
  1234. { .cmd = OVS_FLOW_CMD_NEW,
  1235. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1236. .policy = flow_policy,
  1237. .doit = ovs_flow_cmd_new
  1238. },
  1239. { .cmd = OVS_FLOW_CMD_DEL,
  1240. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1241. .policy = flow_policy,
  1242. .doit = ovs_flow_cmd_del
  1243. },
  1244. { .cmd = OVS_FLOW_CMD_GET,
  1245. .flags = 0, /* OK for unprivileged users. */
  1246. .policy = flow_policy,
  1247. .doit = ovs_flow_cmd_get,
  1248. .dumpit = ovs_flow_cmd_dump
  1249. },
  1250. { .cmd = OVS_FLOW_CMD_SET,
  1251. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1252. .policy = flow_policy,
  1253. .doit = ovs_flow_cmd_set,
  1254. },
  1255. };
  1256. static struct genl_family dp_flow_genl_family __ro_after_init = {
  1257. .hdrsize = sizeof(struct ovs_header),
  1258. .name = OVS_FLOW_FAMILY,
  1259. .version = OVS_FLOW_VERSION,
  1260. .maxattr = OVS_FLOW_ATTR_MAX,
  1261. .netnsok = true,
  1262. .parallel_ops = true,
  1263. .ops = dp_flow_genl_ops,
  1264. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1265. .mcgrps = &ovs_dp_flow_multicast_group,
  1266. .n_mcgrps = 1,
  1267. .module = THIS_MODULE,
  1268. };
  1269. static size_t ovs_dp_cmd_msg_size(void)
  1270. {
  1271. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1272. msgsize += nla_total_size(IFNAMSIZ);
  1273. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
  1274. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
  1275. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1276. return msgsize;
  1277. }
  1278. /* Called with ovs_mutex. */
  1279. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1280. u32 portid, u32 seq, u32 flags, u8 cmd)
  1281. {
  1282. struct ovs_header *ovs_header;
  1283. struct ovs_dp_stats dp_stats;
  1284. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1285. int err;
  1286. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1287. flags, cmd);
  1288. if (!ovs_header)
  1289. goto error;
  1290. ovs_header->dp_ifindex = get_dpifindex(dp);
  1291. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1292. if (err)
  1293. goto nla_put_failure;
  1294. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1295. if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1296. &dp_stats, OVS_DP_ATTR_PAD))
  1297. goto nla_put_failure;
  1298. if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1299. sizeof(struct ovs_dp_megaflow_stats),
  1300. &dp_megaflow_stats, OVS_DP_ATTR_PAD))
  1301. goto nla_put_failure;
  1302. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1303. goto nla_put_failure;
  1304. genlmsg_end(skb, ovs_header);
  1305. return 0;
  1306. nla_put_failure:
  1307. genlmsg_cancel(skb, ovs_header);
  1308. error:
  1309. return -EMSGSIZE;
  1310. }
  1311. static struct sk_buff *ovs_dp_cmd_alloc_info(void)
  1312. {
  1313. return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1314. }
  1315. /* Called with rcu_read_lock or ovs_mutex. */
  1316. static struct datapath *lookup_datapath(struct net *net,
  1317. const struct ovs_header *ovs_header,
  1318. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1319. {
  1320. struct datapath *dp;
  1321. if (!a[OVS_DP_ATTR_NAME])
  1322. dp = get_dp(net, ovs_header->dp_ifindex);
  1323. else {
  1324. struct vport *vport;
  1325. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1326. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1327. }
  1328. return dp ? dp : ERR_PTR(-ENODEV);
  1329. }
  1330. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1331. {
  1332. struct datapath *dp;
  1333. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1334. if (IS_ERR(dp))
  1335. return;
  1336. WARN(dp->user_features, "Dropping previously announced user features\n");
  1337. dp->user_features = 0;
  1338. }
  1339. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1340. {
  1341. if (a[OVS_DP_ATTR_USER_FEATURES])
  1342. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1343. }
  1344. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1345. {
  1346. struct nlattr **a = info->attrs;
  1347. struct vport_parms parms;
  1348. struct sk_buff *reply;
  1349. struct datapath *dp;
  1350. struct vport *vport;
  1351. struct ovs_net *ovs_net;
  1352. int err, i;
  1353. err = -EINVAL;
  1354. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1355. goto err;
  1356. reply = ovs_dp_cmd_alloc_info();
  1357. if (!reply)
  1358. return -ENOMEM;
  1359. err = -ENOMEM;
  1360. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1361. if (dp == NULL)
  1362. goto err_free_reply;
  1363. ovs_dp_set_net(dp, sock_net(skb->sk));
  1364. /* Allocate table. */
  1365. err = ovs_flow_tbl_init(&dp->table);
  1366. if (err)
  1367. goto err_free_dp;
  1368. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1369. if (!dp->stats_percpu) {
  1370. err = -ENOMEM;
  1371. goto err_destroy_table;
  1372. }
  1373. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1374. GFP_KERNEL);
  1375. if (!dp->ports) {
  1376. err = -ENOMEM;
  1377. goto err_destroy_percpu;
  1378. }
  1379. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1380. INIT_HLIST_HEAD(&dp->ports[i]);
  1381. /* Set up our datapath device. */
  1382. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1383. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1384. parms.options = NULL;
  1385. parms.dp = dp;
  1386. parms.port_no = OVSP_LOCAL;
  1387. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1388. ovs_dp_change(dp, a);
  1389. /* So far only local changes have been made, now need the lock. */
  1390. ovs_lock();
  1391. vport = new_vport(&parms);
  1392. if (IS_ERR(vport)) {
  1393. err = PTR_ERR(vport);
  1394. if (err == -EBUSY)
  1395. err = -EEXIST;
  1396. if (err == -EEXIST) {
  1397. /* An outdated user space instance that does not understand
  1398. * the concept of user_features has attempted to create a new
  1399. * datapath and is likely to reuse it. Drop all user features.
  1400. */
  1401. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1402. ovs_dp_reset_user_features(skb, info);
  1403. }
  1404. goto err_destroy_ports_array;
  1405. }
  1406. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1407. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1408. BUG_ON(err < 0);
  1409. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1410. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1411. ovs_unlock();
  1412. ovs_notify(&dp_datapath_genl_family, reply, info);
  1413. return 0;
  1414. err_destroy_ports_array:
  1415. ovs_unlock();
  1416. kfree(dp->ports);
  1417. err_destroy_percpu:
  1418. free_percpu(dp->stats_percpu);
  1419. err_destroy_table:
  1420. ovs_flow_tbl_destroy(&dp->table);
  1421. err_free_dp:
  1422. kfree(dp);
  1423. err_free_reply:
  1424. kfree_skb(reply);
  1425. err:
  1426. return err;
  1427. }
  1428. /* Called with ovs_mutex. */
  1429. static void __dp_destroy(struct datapath *dp)
  1430. {
  1431. int i;
  1432. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1433. struct vport *vport;
  1434. struct hlist_node *n;
  1435. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1436. if (vport->port_no != OVSP_LOCAL)
  1437. ovs_dp_detach_port(vport);
  1438. }
  1439. list_del_rcu(&dp->list_node);
  1440. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1441. * all ports in datapath are destroyed first before freeing datapath.
  1442. */
  1443. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1444. /* RCU destroy the flow table */
  1445. call_rcu(&dp->rcu, destroy_dp_rcu);
  1446. }
  1447. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1448. {
  1449. struct sk_buff *reply;
  1450. struct datapath *dp;
  1451. int err;
  1452. reply = ovs_dp_cmd_alloc_info();
  1453. if (!reply)
  1454. return -ENOMEM;
  1455. ovs_lock();
  1456. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1457. err = PTR_ERR(dp);
  1458. if (IS_ERR(dp))
  1459. goto err_unlock_free;
  1460. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1461. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1462. BUG_ON(err < 0);
  1463. __dp_destroy(dp);
  1464. ovs_unlock();
  1465. ovs_notify(&dp_datapath_genl_family, reply, info);
  1466. return 0;
  1467. err_unlock_free:
  1468. ovs_unlock();
  1469. kfree_skb(reply);
  1470. return err;
  1471. }
  1472. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1473. {
  1474. struct sk_buff *reply;
  1475. struct datapath *dp;
  1476. int err;
  1477. reply = ovs_dp_cmd_alloc_info();
  1478. if (!reply)
  1479. return -ENOMEM;
  1480. ovs_lock();
  1481. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1482. err = PTR_ERR(dp);
  1483. if (IS_ERR(dp))
  1484. goto err_unlock_free;
  1485. ovs_dp_change(dp, info->attrs);
  1486. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1487. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1488. BUG_ON(err < 0);
  1489. ovs_unlock();
  1490. ovs_notify(&dp_datapath_genl_family, reply, info);
  1491. return 0;
  1492. err_unlock_free:
  1493. ovs_unlock();
  1494. kfree_skb(reply);
  1495. return err;
  1496. }
  1497. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1498. {
  1499. struct sk_buff *reply;
  1500. struct datapath *dp;
  1501. int err;
  1502. reply = ovs_dp_cmd_alloc_info();
  1503. if (!reply)
  1504. return -ENOMEM;
  1505. ovs_lock();
  1506. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1507. if (IS_ERR(dp)) {
  1508. err = PTR_ERR(dp);
  1509. goto err_unlock_free;
  1510. }
  1511. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1512. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1513. BUG_ON(err < 0);
  1514. ovs_unlock();
  1515. return genlmsg_reply(reply, info);
  1516. err_unlock_free:
  1517. ovs_unlock();
  1518. kfree_skb(reply);
  1519. return err;
  1520. }
  1521. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1522. {
  1523. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1524. struct datapath *dp;
  1525. int skip = cb->args[0];
  1526. int i = 0;
  1527. ovs_lock();
  1528. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1529. if (i >= skip &&
  1530. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1531. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1532. OVS_DP_CMD_NEW) < 0)
  1533. break;
  1534. i++;
  1535. }
  1536. ovs_unlock();
  1537. cb->args[0] = i;
  1538. return skb->len;
  1539. }
  1540. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1541. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1542. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1543. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1544. };
  1545. static const struct genl_ops dp_datapath_genl_ops[] = {
  1546. { .cmd = OVS_DP_CMD_NEW,
  1547. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1548. .policy = datapath_policy,
  1549. .doit = ovs_dp_cmd_new
  1550. },
  1551. { .cmd = OVS_DP_CMD_DEL,
  1552. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1553. .policy = datapath_policy,
  1554. .doit = ovs_dp_cmd_del
  1555. },
  1556. { .cmd = OVS_DP_CMD_GET,
  1557. .flags = 0, /* OK for unprivileged users. */
  1558. .policy = datapath_policy,
  1559. .doit = ovs_dp_cmd_get,
  1560. .dumpit = ovs_dp_cmd_dump
  1561. },
  1562. { .cmd = OVS_DP_CMD_SET,
  1563. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1564. .policy = datapath_policy,
  1565. .doit = ovs_dp_cmd_set,
  1566. },
  1567. };
  1568. static struct genl_family dp_datapath_genl_family __ro_after_init = {
  1569. .hdrsize = sizeof(struct ovs_header),
  1570. .name = OVS_DATAPATH_FAMILY,
  1571. .version = OVS_DATAPATH_VERSION,
  1572. .maxattr = OVS_DP_ATTR_MAX,
  1573. .netnsok = true,
  1574. .parallel_ops = true,
  1575. .ops = dp_datapath_genl_ops,
  1576. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1577. .mcgrps = &ovs_dp_datapath_multicast_group,
  1578. .n_mcgrps = 1,
  1579. .module = THIS_MODULE,
  1580. };
  1581. /* Called with ovs_mutex or RCU read lock. */
  1582. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1583. u32 portid, u32 seq, u32 flags, u8 cmd)
  1584. {
  1585. struct ovs_header *ovs_header;
  1586. struct ovs_vport_stats vport_stats;
  1587. int err;
  1588. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1589. flags, cmd);
  1590. if (!ovs_header)
  1591. return -EMSGSIZE;
  1592. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1593. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1594. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1595. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1596. ovs_vport_name(vport)))
  1597. goto nla_put_failure;
  1598. ovs_vport_get_stats(vport, &vport_stats);
  1599. if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
  1600. sizeof(struct ovs_vport_stats), &vport_stats,
  1601. OVS_VPORT_ATTR_PAD))
  1602. goto nla_put_failure;
  1603. if (ovs_vport_get_upcall_portids(vport, skb))
  1604. goto nla_put_failure;
  1605. err = ovs_vport_get_options(vport, skb);
  1606. if (err == -EMSGSIZE)
  1607. goto error;
  1608. genlmsg_end(skb, ovs_header);
  1609. return 0;
  1610. nla_put_failure:
  1611. err = -EMSGSIZE;
  1612. error:
  1613. genlmsg_cancel(skb, ovs_header);
  1614. return err;
  1615. }
  1616. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1617. {
  1618. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1619. }
  1620. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1621. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1622. u32 seq, u8 cmd)
  1623. {
  1624. struct sk_buff *skb;
  1625. int retval;
  1626. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1627. if (!skb)
  1628. return ERR_PTR(-ENOMEM);
  1629. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1630. BUG_ON(retval < 0);
  1631. return skb;
  1632. }
  1633. /* Called with ovs_mutex or RCU read lock. */
  1634. static struct vport *lookup_vport(struct net *net,
  1635. const struct ovs_header *ovs_header,
  1636. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1637. {
  1638. struct datapath *dp;
  1639. struct vport *vport;
  1640. if (a[OVS_VPORT_ATTR_NAME]) {
  1641. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1642. if (!vport)
  1643. return ERR_PTR(-ENODEV);
  1644. if (ovs_header->dp_ifindex &&
  1645. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1646. return ERR_PTR(-ENODEV);
  1647. return vport;
  1648. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1649. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1650. if (port_no >= DP_MAX_PORTS)
  1651. return ERR_PTR(-EFBIG);
  1652. dp = get_dp(net, ovs_header->dp_ifindex);
  1653. if (!dp)
  1654. return ERR_PTR(-ENODEV);
  1655. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1656. if (!vport)
  1657. return ERR_PTR(-ENODEV);
  1658. return vport;
  1659. } else
  1660. return ERR_PTR(-EINVAL);
  1661. }
  1662. /* Called with ovs_mutex */
  1663. static void update_headroom(struct datapath *dp)
  1664. {
  1665. unsigned dev_headroom, max_headroom = 0;
  1666. struct net_device *dev;
  1667. struct vport *vport;
  1668. int i;
  1669. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1670. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1671. dev = vport->dev;
  1672. dev_headroom = netdev_get_fwd_headroom(dev);
  1673. if (dev_headroom > max_headroom)
  1674. max_headroom = dev_headroom;
  1675. }
  1676. }
  1677. dp->max_headroom = max_headroom;
  1678. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1679. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
  1680. netdev_set_rx_headroom(vport->dev, max_headroom);
  1681. }
  1682. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1683. {
  1684. struct nlattr **a = info->attrs;
  1685. struct ovs_header *ovs_header = info->userhdr;
  1686. struct vport_parms parms;
  1687. struct sk_buff *reply;
  1688. struct vport *vport;
  1689. struct datapath *dp;
  1690. u32 port_no;
  1691. int err;
  1692. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1693. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1694. return -EINVAL;
  1695. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1696. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1697. if (port_no >= DP_MAX_PORTS)
  1698. return -EFBIG;
  1699. reply = ovs_vport_cmd_alloc_info();
  1700. if (!reply)
  1701. return -ENOMEM;
  1702. ovs_lock();
  1703. restart:
  1704. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1705. err = -ENODEV;
  1706. if (!dp)
  1707. goto exit_unlock_free;
  1708. if (port_no) {
  1709. vport = ovs_vport_ovsl(dp, port_no);
  1710. err = -EBUSY;
  1711. if (vport)
  1712. goto exit_unlock_free;
  1713. } else {
  1714. for (port_no = 1; ; port_no++) {
  1715. if (port_no >= DP_MAX_PORTS) {
  1716. err = -EFBIG;
  1717. goto exit_unlock_free;
  1718. }
  1719. vport = ovs_vport_ovsl(dp, port_no);
  1720. if (!vport)
  1721. break;
  1722. }
  1723. }
  1724. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1725. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1726. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1727. parms.dp = dp;
  1728. parms.port_no = port_no;
  1729. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1730. vport = new_vport(&parms);
  1731. err = PTR_ERR(vport);
  1732. if (IS_ERR(vport)) {
  1733. if (err == -EAGAIN)
  1734. goto restart;
  1735. goto exit_unlock_free;
  1736. }
  1737. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1738. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1739. if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
  1740. update_headroom(dp);
  1741. else
  1742. netdev_set_rx_headroom(vport->dev, dp->max_headroom);
  1743. BUG_ON(err < 0);
  1744. ovs_unlock();
  1745. ovs_notify(&dp_vport_genl_family, reply, info);
  1746. return 0;
  1747. exit_unlock_free:
  1748. ovs_unlock();
  1749. kfree_skb(reply);
  1750. return err;
  1751. }
  1752. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1753. {
  1754. struct nlattr **a = info->attrs;
  1755. struct sk_buff *reply;
  1756. struct vport *vport;
  1757. int err;
  1758. reply = ovs_vport_cmd_alloc_info();
  1759. if (!reply)
  1760. return -ENOMEM;
  1761. ovs_lock();
  1762. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1763. err = PTR_ERR(vport);
  1764. if (IS_ERR(vport))
  1765. goto exit_unlock_free;
  1766. if (a[OVS_VPORT_ATTR_TYPE] &&
  1767. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1768. err = -EINVAL;
  1769. goto exit_unlock_free;
  1770. }
  1771. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1772. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1773. if (err)
  1774. goto exit_unlock_free;
  1775. }
  1776. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1777. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1778. err = ovs_vport_set_upcall_portids(vport, ids);
  1779. if (err)
  1780. goto exit_unlock_free;
  1781. }
  1782. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1783. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1784. BUG_ON(err < 0);
  1785. ovs_unlock();
  1786. ovs_notify(&dp_vport_genl_family, reply, info);
  1787. return 0;
  1788. exit_unlock_free:
  1789. ovs_unlock();
  1790. kfree_skb(reply);
  1791. return err;
  1792. }
  1793. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1794. {
  1795. bool must_update_headroom = false;
  1796. struct nlattr **a = info->attrs;
  1797. struct sk_buff *reply;
  1798. struct datapath *dp;
  1799. struct vport *vport;
  1800. int err;
  1801. reply = ovs_vport_cmd_alloc_info();
  1802. if (!reply)
  1803. return -ENOMEM;
  1804. ovs_lock();
  1805. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1806. err = PTR_ERR(vport);
  1807. if (IS_ERR(vport))
  1808. goto exit_unlock_free;
  1809. if (vport->port_no == OVSP_LOCAL) {
  1810. err = -EINVAL;
  1811. goto exit_unlock_free;
  1812. }
  1813. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1814. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1815. BUG_ON(err < 0);
  1816. /* the vport deletion may trigger dp headroom update */
  1817. dp = vport->dp;
  1818. if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
  1819. must_update_headroom = true;
  1820. netdev_reset_rx_headroom(vport->dev);
  1821. ovs_dp_detach_port(vport);
  1822. if (must_update_headroom)
  1823. update_headroom(dp);
  1824. ovs_unlock();
  1825. ovs_notify(&dp_vport_genl_family, reply, info);
  1826. return 0;
  1827. exit_unlock_free:
  1828. ovs_unlock();
  1829. kfree_skb(reply);
  1830. return err;
  1831. }
  1832. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1833. {
  1834. struct nlattr **a = info->attrs;
  1835. struct ovs_header *ovs_header = info->userhdr;
  1836. struct sk_buff *reply;
  1837. struct vport *vport;
  1838. int err;
  1839. reply = ovs_vport_cmd_alloc_info();
  1840. if (!reply)
  1841. return -ENOMEM;
  1842. rcu_read_lock();
  1843. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1844. err = PTR_ERR(vport);
  1845. if (IS_ERR(vport))
  1846. goto exit_unlock_free;
  1847. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1848. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1849. BUG_ON(err < 0);
  1850. rcu_read_unlock();
  1851. return genlmsg_reply(reply, info);
  1852. exit_unlock_free:
  1853. rcu_read_unlock();
  1854. kfree_skb(reply);
  1855. return err;
  1856. }
  1857. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1858. {
  1859. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1860. struct datapath *dp;
  1861. int bucket = cb->args[0], skip = cb->args[1];
  1862. int i, j = 0;
  1863. rcu_read_lock();
  1864. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1865. if (!dp) {
  1866. rcu_read_unlock();
  1867. return -ENODEV;
  1868. }
  1869. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1870. struct vport *vport;
  1871. j = 0;
  1872. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1873. if (j >= skip &&
  1874. ovs_vport_cmd_fill_info(vport, skb,
  1875. NETLINK_CB(cb->skb).portid,
  1876. cb->nlh->nlmsg_seq,
  1877. NLM_F_MULTI,
  1878. OVS_VPORT_CMD_NEW) < 0)
  1879. goto out;
  1880. j++;
  1881. }
  1882. skip = 0;
  1883. }
  1884. out:
  1885. rcu_read_unlock();
  1886. cb->args[0] = i;
  1887. cb->args[1] = j;
  1888. return skb->len;
  1889. }
  1890. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1891. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1892. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1893. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1894. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1895. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
  1896. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1897. };
  1898. static const struct genl_ops dp_vport_genl_ops[] = {
  1899. { .cmd = OVS_VPORT_CMD_NEW,
  1900. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1901. .policy = vport_policy,
  1902. .doit = ovs_vport_cmd_new
  1903. },
  1904. { .cmd = OVS_VPORT_CMD_DEL,
  1905. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1906. .policy = vport_policy,
  1907. .doit = ovs_vport_cmd_del
  1908. },
  1909. { .cmd = OVS_VPORT_CMD_GET,
  1910. .flags = 0, /* OK for unprivileged users. */
  1911. .policy = vport_policy,
  1912. .doit = ovs_vport_cmd_get,
  1913. .dumpit = ovs_vport_cmd_dump
  1914. },
  1915. { .cmd = OVS_VPORT_CMD_SET,
  1916. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1917. .policy = vport_policy,
  1918. .doit = ovs_vport_cmd_set,
  1919. },
  1920. };
  1921. struct genl_family dp_vport_genl_family __ro_after_init = {
  1922. .hdrsize = sizeof(struct ovs_header),
  1923. .name = OVS_VPORT_FAMILY,
  1924. .version = OVS_VPORT_VERSION,
  1925. .maxattr = OVS_VPORT_ATTR_MAX,
  1926. .netnsok = true,
  1927. .parallel_ops = true,
  1928. .ops = dp_vport_genl_ops,
  1929. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1930. .mcgrps = &ovs_dp_vport_multicast_group,
  1931. .n_mcgrps = 1,
  1932. .module = THIS_MODULE,
  1933. };
  1934. static struct genl_family * const dp_genl_families[] = {
  1935. &dp_datapath_genl_family,
  1936. &dp_vport_genl_family,
  1937. &dp_flow_genl_family,
  1938. &dp_packet_genl_family,
  1939. };
  1940. static void dp_unregister_genl(int n_families)
  1941. {
  1942. int i;
  1943. for (i = 0; i < n_families; i++)
  1944. genl_unregister_family(dp_genl_families[i]);
  1945. }
  1946. static int __init dp_register_genl(void)
  1947. {
  1948. int err;
  1949. int i;
  1950. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1951. err = genl_register_family(dp_genl_families[i]);
  1952. if (err)
  1953. goto error;
  1954. }
  1955. return 0;
  1956. error:
  1957. dp_unregister_genl(i);
  1958. return err;
  1959. }
  1960. static int __net_init ovs_init_net(struct net *net)
  1961. {
  1962. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1963. INIT_LIST_HEAD(&ovs_net->dps);
  1964. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1965. ovs_ct_init(net);
  1966. return 0;
  1967. }
  1968. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1969. struct list_head *head)
  1970. {
  1971. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1972. struct datapath *dp;
  1973. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1974. int i;
  1975. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1976. struct vport *vport;
  1977. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1978. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1979. continue;
  1980. if (dev_net(vport->dev) == dnet)
  1981. list_add(&vport->detach_list, head);
  1982. }
  1983. }
  1984. }
  1985. }
  1986. static void __net_exit ovs_exit_net(struct net *dnet)
  1987. {
  1988. struct datapath *dp, *dp_next;
  1989. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1990. struct vport *vport, *vport_next;
  1991. struct net *net;
  1992. LIST_HEAD(head);
  1993. ovs_ct_exit(dnet);
  1994. ovs_lock();
  1995. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1996. __dp_destroy(dp);
  1997. rtnl_lock();
  1998. for_each_net(net)
  1999. list_vports_from_net(net, dnet, &head);
  2000. rtnl_unlock();
  2001. /* Detach all vports from given namespace. */
  2002. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  2003. list_del(&vport->detach_list);
  2004. ovs_dp_detach_port(vport);
  2005. }
  2006. ovs_unlock();
  2007. cancel_work_sync(&ovs_net->dp_notify_work);
  2008. }
  2009. static struct pernet_operations ovs_net_ops = {
  2010. .init = ovs_init_net,
  2011. .exit = ovs_exit_net,
  2012. .id = &ovs_net_id,
  2013. .size = sizeof(struct ovs_net),
  2014. };
  2015. static int __init dp_init(void)
  2016. {
  2017. int err;
  2018. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  2019. pr_info("Open vSwitch switching datapath\n");
  2020. err = action_fifos_init();
  2021. if (err)
  2022. goto error;
  2023. err = ovs_internal_dev_rtnl_link_register();
  2024. if (err)
  2025. goto error_action_fifos_exit;
  2026. err = ovs_flow_init();
  2027. if (err)
  2028. goto error_unreg_rtnl_link;
  2029. err = ovs_vport_init();
  2030. if (err)
  2031. goto error_flow_exit;
  2032. err = register_pernet_device(&ovs_net_ops);
  2033. if (err)
  2034. goto error_vport_exit;
  2035. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  2036. if (err)
  2037. goto error_netns_exit;
  2038. err = ovs_netdev_init();
  2039. if (err)
  2040. goto error_unreg_notifier;
  2041. err = dp_register_genl();
  2042. if (err < 0)
  2043. goto error_unreg_netdev;
  2044. return 0;
  2045. error_unreg_netdev:
  2046. ovs_netdev_exit();
  2047. error_unreg_notifier:
  2048. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2049. error_netns_exit:
  2050. unregister_pernet_device(&ovs_net_ops);
  2051. error_vport_exit:
  2052. ovs_vport_exit();
  2053. error_flow_exit:
  2054. ovs_flow_exit();
  2055. error_unreg_rtnl_link:
  2056. ovs_internal_dev_rtnl_link_unregister();
  2057. error_action_fifos_exit:
  2058. action_fifos_exit();
  2059. error:
  2060. return err;
  2061. }
  2062. static void dp_cleanup(void)
  2063. {
  2064. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2065. ovs_netdev_exit();
  2066. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2067. unregister_pernet_device(&ovs_net_ops);
  2068. rcu_barrier();
  2069. ovs_vport_exit();
  2070. ovs_flow_exit();
  2071. ovs_internal_dev_rtnl_link_unregister();
  2072. action_fifos_exit();
  2073. }
  2074. module_init(dp_init);
  2075. module_exit(dp_cleanup);
  2076. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2077. MODULE_LICENSE("GPL");
  2078. MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
  2079. MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
  2080. MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
  2081. MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);