datapath.c 59 KB

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